string-formatting

UnknownFormatConversionException is caused by symbol '%' in String.format()

萝らか妹 提交于 2019-12-10 01:16:03
问题 String template = "%s and '%'"; String result = String.format(template, "my string"); System.out.println(result); Expected : my string and '%' But result is : java.util.UnknownFormatConversionException: Conversion = ''' Why? How to correctly declared the sequence '%' so that it's ignored by String.format() ? 回答1: % is already used by format specifiers so it requires an additional % to display that character: String template = "%s and '%%'"; 来源: https://stackoverflow.com/questions/16713396

Calling ToString(“YYYY-mm-dd”) results in wrong date format

Deadly 提交于 2019-12-10 01:12:00
问题 I've got a constructor which takes a DateTime object: public Report(DateTime date, string start = "0", string end = "0") { Logger.Info("Creating a new Report..."); StartTime = start; EndTime = end; Date = date.ToString("YYYY-mm-dd"); SetStartEndTimes(); Logger.Info("Report Created"); } Now, this was working fine just 3 days ago. However, I come back today, after a break, and this is the results I'm seeing: As you can see, the date being passed in is right. However, after the format, it is not

Format a number containing a decimal point with leading zeroes

你离开我真会死。 提交于 2019-12-09 16:24:03
问题 I want to format a number with a decimal point in it with leading zeros. This >>> '3.3'.zfill(5) 003.3 considers all the digits and even the decimal point. Is there a function in python that considers only the whole part? I only need to format simple numbers with no more than five decimal places. Also, using %5f seems to consider trailing instead of leading zeros. 回答1: Starting with a string as your example does, you could write a small function such as this to do what you want: def zpad(val,

Format TimeSpan to mm:ss for positive and negative TimeSpans

。_饼干妹妹 提交于 2019-12-09 15:45:26
问题 I'm looking for a solution in .net 3.5 I wrote the following working solution: private string FormatTimeSpan(TimeSpan time) { return String.Format("{0}{1:00}:{2:00}", time < TimeSpan.Zero ? "-" : "", Math.Abs(time.Minutes), Math.Abs(time.Seconds)); } But my Question is: Is there a better way? Maybe something shorter where I do not need an helper function. 回答1: Somewhat shorter, using Custom TimeSpan Format Strings: private string FormatTimeSpan(TimeSpan time) { return ((time < TimeSpan.Zero)

Format a Social Security Number (SSN) as XXX-XX-XXXX from XXXXXXXXX

百般思念 提交于 2019-12-09 05:17:01
问题 I am getting a social security number (SSN) from a data warehouse. While posting it to a CRM I want it to be formatted like XXX-XX-XXXX instead of XXXXXXXXX . It's like converting a simple string with dashes at positions 4 and 7 . I am pretty new to C#, so what is the best way to do this? 回答1: Check out the String.Insert method. string formattedSSN = unformattedSSN.Insert(5, "-").Insert(3, "-"); 回答2: For a simple, short, and self commenting solution, try: String.Format("{0:000-00-0000}",

Javascript phone mask for text field with regex

老子叫甜甜 提交于 2019-12-09 03:51:29
问题 I'm using this function to phone mask and works almost perfectly. function mask(o, f) { v_obj = o; v_fun = f; setTimeout("execmask()", 1) }; function execmask() { v_obj.value = v_fun(v_obj.value) }; function mphone(v){ v=v.replace(/\D/g,""); v=v.substring(0, 11); v=v.replace(/^(\d{2})(\d)/g,"(OXX$1) $2"); v=v.replace(/(\d)(\d{4})$/,"$1-$2"); return v; } Here I run the mask in the text field: <input type="text" id="phone" name="phone" onkeypress="mask(this, mphone);" onblur="mask(this, mphone)

python - How to format variable number of arguments into a string?

只愿长相守 提交于 2019-12-09 02:32:46
问题 We know that formatting one argument can be done using one %s in a string: >>> "Hello %s" % "world" 'Hello world' for two arguments, we can use two %s (duh!): >>> "Hello %s, %s" % ("John", "Joe") 'Hello John, Joe' So, how can I format a variable number of arguments without having to explicitly define within the base string a number of %s equal to the number of arguments to format ? it would be very cool if something like this exists: >>> "Hello <cool_operator_here>" % ("John", "Joe", "Mary")

Safe Parsing of Format Directives in Common Lisp

怎甘沉沦 提交于 2019-12-08 20:21:06
问题 I would like to read in a string from an input file (which may or may not have been modified by the user). I would like to treat this string as a format directive to be called with a fixed number of arguments. However, I understand that some format directives (particularly, the ~/ comes to mind) could potentially be used to inject function calls, making this approach inherently unsafe. When using read to parse data in Common Lisp, the language provides the *read-eval* dynamic variable which

short form for string.format(…,**locals())

一世执手 提交于 2019-12-08 18:10:35
问题 I usually use the following pattern (as mentioned in this question): a=1 s= "{a}".format(**locals()) I think it's a great way to write easily readable code. Sometimes it's useful to "chain" string formats, in order to "modularize" the creation of complex strings: a="1" b="2" c="{a}+{b}".format(**locals()) d="{c} is a sum".format(**locals()) #d=="1+2 is a sum" Pretty soon, the code is pestered with X.format(**locals()) . To solve this problem, I tried to create a lambda: f= lambda x: x.format(

sys.exc_info()[1] type and format in Python 2.71

纵然是瞬间 提交于 2019-12-08 13:15:05
问题 In python 2.71 on Windows XP I need to use FTP. My code is : try: ftp = FTP(trec.address) ftp.login(trec.login, trec.passw) s = ftp.retrlines('LIST ' + trec.filetype) ftp.quit() except: (type, value, tb) = sys.exc_info() reponse = "%s" % value But I have an error on the last line : UnicodeDecodeError: 'ascii' codec can't decode byte 0xea in position 38: ordinal not in range(128) As I am in French Windows env. the sys.exc_info()[1] is : [Errno 10061] Aucune connexion n'a pu être établie car l