string-formatting

difference between MessageFormat.format and String.format in jdk1.5?

我只是一个虾纸丫 提交于 2019-12-03 03:25:34
问题 What is the difference between MessageFormat.format and String.format in JDK 1.5? 回答1: Put simply, the main difference is in format string: MessageFormat.format() format string accepts argument positions (eg. {0} , {1} ). Example: "This is year {0}!" The developer doesn't have to worry about argument types, because they are, most often, recognized and formated according to current Locale . String.format() format string accepts argument type specifiers (eg. %d for numbers, %s for strings).

What are the supported Swift String format specifiers?

本小妞迷上赌 提交于 2019-12-03 03:21:35
问题 In Swift, I can format a String with format specifiers: // This will return "0.120" String(format: "%.03f", 0.12) But the official documentation is not giving any information or link regarding the supported format specifiers or how to build a template similar to "%.03f" : https://developer.apple.com/documentation/swift/string/3126742-init It only says: Returns a String object initialized by using a given format string as a template into which the remaining argument values are substituted. 回答1

How to use StringFormat in XAML elements?

試著忘記壹切 提交于 2019-12-03 02:57:04
问题 I'm deep in a XAML stack of elements binding to orders. The order date displays as e.g. "12/31/2008 12:00:00 AM". I want it to display as e.g. "31.12.2008". How can I do this? I have seen other stackoverflow questions mention StringFormat but they use multibinding in ways that I can't get to work. Here is the kind of syntax I would like (this is pseudocode), simply specifying StringFormat where you need it, is this possible somehow? <StackPanel> <ListView ItemsSource="{Binding Orders}">

Adding an extension method to the string class - C#

江枫思渺然 提交于 2019-12-03 02:10:34
Not sure what I'm doing wrong here. The extension method is not recognized. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using StringExtensions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { RunTests(); } static void RunTests() { try { ///SafeFormat SafeFormat("Hi There"); SafeFormat("test {0}", "value"); SafeFormat("test missing second value {0} - {1}", "test1"); SafeFormat("{0}"); //regular format RegularFormat("Hi There"); RegularFormat("test {0}", "value"); RegularFormat("test

Join list of string to comma separated and enclosed in single quotes

一笑奈何 提交于 2019-12-03 02:07:23
List<string> test = new List<string>(); test.Add("test's"); test.Add("test"); test.Add("test's more"); string s = string.Format("'{0}'", string.Join("','", test)); now the s is 'test's','test','test's more' but I need to replace the inner quotes with 2 single quotes like this: 'test''s','test','test''s more' update: I got it to work as below, but I would prefer a cleaner way if possible. string s = string.Format("`{0}`", string.Join("`,`", test)).Replace("'", "''").Replace("`", "'"); This should work: List<string> test = new List<string>(); test.Add("test's"); test.Add("test"); test.Add("test

Preventing decimals with NSNumberFormatter

可紊 提交于 2019-12-03 01:39:33
I have an NSNumberFormatter which I'm trying to use to generate a whole number of GBP (£) from an NSNumber. I keep getting two decimal places regardless of which incantation I try. My code is: NSNumberFormatter *fmtCurrency = [[[NSNumberFormatter alloc] init] autorelease]; [fmtCurrency setNumberStyle: NSNumberFormatterCurrencyStyle]; [fmtCurrency setGeneratesDecimalNumbers:FALSE]; [fmtCurrency setCurrencyCode:@"GBP"]; [fmtCurrency setCurrencySymbol:@"£"]; txtTotal.text = [fmtCurrency stringFromNumber: result.Bill ]; // this is an NSNumber zpasternack I think you want to call

How are booleans formatted in Strings in Python?

谁都会走 提交于 2019-12-03 01:14:05
问题 I see I can't do: "%b %b" % (True, False) in Python. I guessed %b for b(oolean). Is there something like this? 回答1: >>> print "%r, %r" % (True, False) True, False This is not specific to boolean values - %r calls the __repr__ method on the argument. %s (for str ) should also work. 回答2: If you want True False use: "%s %s" % (True, False) because str(True) is 'True' and str(False) is 'False' . or if you want 1 0 use: "%i %i" % (True, False) because int(True) is 1 and int(False) is 0 . 回答3: You

Fixed width number formatting python 3

核能气质少年 提交于 2019-12-02 23:44:15
How do I get an integer to fill 0's to a fixed width in python 3.2 using the format attribute? Example: a = 1 print('{0:3}'.format(a)} gives ' 1' instead of '001' I want. In python 2.x, I know that this can be done using print "%03d" % number. I checked the python 3 string documentation but wasn't able to get this. http://docs.python.org/release/3.2/library/string.html#format-specification-mini-language Thanks. Prefix the width with a 0 : >>> '{0:03}'.format(1) '001' Also, you don't need the place-marker in recent versions of Python (not sure which, but at least 2.7 and 3.1): >>> '{:03}'

advanced string formatting vs template strings

二次信任 提交于 2019-12-02 21:40:39
I was wondering if there is a advantage of using template strings instead of the new advanced string formatting ? Templates are meant to be simpler than the the usual string formatting, at the cost of expressiveness. The rationale of PEP 292 compares templates to Python's % -style string formatting: Python currently supports a string substitution syntax based on C's printf() '%' formatting character. While quite rich, %-formatting codes are also error prone, even for experienced Python programmers. A common mistake is to leave off the trailing format character, e.g. the s in %(name)s . In

Python: Capitalize a word using string.format()

余生颓废 提交于 2019-12-02 20:50:12
Is it possible to capitalize a word using string formatting? For example, "{user} did such and such.".format(user="foobar") should return "Foobar did such and such." Note that I'm well aware of .capitalize() ; however, here's a (very simplified version of) code I'm using: printme = random.choice(["On {date}, {user} did la-dee-dah. ", "{user} did la-dee-dah on {date}. " ]) output = printme.format(user=x,date=y) As you can see, just defining user as x.capitalize() in the .format() doesn't work, since then it would also be applied (incorrectly) to the first scenario. And since I can't predict