string-formatting

String format with optional dict key-value

时间秒杀一切 提交于 2019-11-29 13:56:27
Is there any way to format string with dict but optionally without key errors? This works fine: opening_line = '%(greetings)s %(name)s !!!' opening_line % {'greetings': 'hello', 'name': 'john'} But let's say I don't know the name, and I would like to format above line only for 'greetings' . Something like, opening_line % {'greetings': 'hello'} Output would be fine even if: 'hii %(name)s !!!' # keeping name un-formatted But this gives KeyError while unpacking Is there any way? Use defaultdict , this will allow you to specify a default value for keys which don't exist in the dictionary. For

Wpf Binding Stringformat to show only first character

拈花ヽ惹草 提交于 2019-11-29 12:02:27
Is there any way so that i can show only first character of a Bound string on a textblock..? For eg;If i Bind 'Male', my textblock should only show 'M'..... You might use a value converter to return a string prefix: class PrefixValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string s = value.ToString(); int prefixLength; if (!int.TryParse(parameter.ToString(), out prefixLength) || s.Length <= prefixLength) { return s; } return s.Substring(0, prefixLength); } public object ConvertBack(object

How to put unprocessed (escaped) words inside String.Format

北战南征 提交于 2019-11-29 11:32:01
问题 I am formatting a date: str = String.Format("{0:MMM d m:mm"+yearStr+"}", dt); I want to put the word "at" after the "d", but I don't want the string to format it. I just want the word "at". How can I achieve this? 回答1: You can surround literal strings with quotes, which for longer strings is probably easier and a bit more readable than escaping every character with a backslash: str = String.Format("{0:MMM d 'at' m:mm"+yearStr+"}", dt); See Custom Date and Time Format Strings in MSDN Library

variable decimal places in .Net string formatters?

ぐ巨炮叔叔 提交于 2019-11-29 11:18:57
问题 Fixed decimal places is easy String.Format("{0:F1}", 654.321); gives 654.3 How do I feed the number of decimal places in as a parameter like you can in C? So String.Format("{0:F?}", 654.321, 2); gives 654.32 I can't find what should replace the ? 回答1: Use NumberFormatInfo : Console.WriteLine(string.Format(new NumberFormatInfo() { NumberDecimalDigits = 2 }, "{0:F}", new decimal(1234.567))); Console.WriteLine(string.Format(new NumberFormatInfo() { NumberDecimalDigits = 7 }, "{0:F}", new decimal

Getting the current time as a YYYY-MM-DD-HH-MM-SS string

左心房为你撑大大i 提交于 2019-11-29 11:12:59
问题 I'm trying to get the current time as a "YYYY-MM-DD-HH-MM-SS" formatted string in an elegant way. I can take the current time in ISO format from Boost's "Date Time" library, but it has other delimiting strings which won't work for me (I'm using this in a filename). Of course I can just replace the delimiting strings, but have a feeling that there's a nicer way to do this with date-time's formatting options. Is there such a way, and if so, how can I use it? 回答1: Use std::strftime, it is

string.Format fails at runtime with array of integers

霸气de小男生 提交于 2019-11-29 11:02:06
问题 Consider string.Format() whose parameters are a string and, among others in the overload list, an object[] or many objects. This statement succeeds: string foo = string.Format("{0} {1}", 5, 6); as does this: object[] myObjs = new object[] {8,9}; string baz = string.Format("{0} and {1}", myObjs; as does an array of strings: string[] myStrings = new string[] {"abc", "xyz"}; string baz = string.Format("{0} {1}", myStrings); It seems that the integers, when specified individually, can be boxed or

How to format localised strings in Swift?

烂漫一生 提交于 2019-11-29 10:47:24
问题 I am learning to localise my app to Simplified Chinese. I am following this tutorial on how to do this. Because the tutorial is based on Obj-C, formatted strings can be written like this: "Yesterday you sold %@ apps" = "Ayer le vendió %@ aplicaciones"; "You like?" = "~Es bueno?~"; But I am using Swift. And in Swift I don't think you can use %@ to indicate that there is something to be placed there. We have string interpolation right? My app is kind of related to maths. And I want to display

undocumented CONVERT styles - datetime 23

╄→尐↘猪︶ㄣ 提交于 2019-11-29 10:18:01
问题 Recently I stumbled upon CONVERT function style 23, which is very handy as it gives you DATE in format yyyy-mm-dd . The problem is that it's not documented in msdn! (link from SSMS help after F1 on CONVERT: http://msdn.microsoft.com/en-us/library/ms187928%28SQL.105%29.aspx). Example: select convert( date ,'2012-01-30', 23) select convert(varchar(255), getdate(), 23) This style is very useful and I've been missing it, but my concerns are: - Is it safe to use? Is it deprecated or sneak in by

Is it more Pythonic to use String Formatting over String Concatenation in Python 3?

青春壹個敷衍的年華 提交于 2019-11-29 08:54:24
So I'm programming a text game in Python 3.4 that requires the use of the print() function very often to display variables to the user. The two ways I've always done this is with string formatting and string concatenation : print('{} has {} health left.'.format(player, health)) And, print(player + ' has ' + str(health) + ' health left.') So which is better? They're both equally as readable and quick to type, and perform exactly the same. Which one is more Pythonic and why? Question asked as I couldn't find an answer for this on Stack Overflow that wasn't concerned with Java. Eddie Depends upon

String.Format(“{0:C2}”, -1234) (Currency format) treats negative numbers as positive

烂漫一生 提交于 2019-11-29 07:22:42
问题 I am using String.Format("{0:C2}", -1234) to format numbers. It always formats the amount to a positive number, while I want it to become $ - 1234 回答1: Am I right in saying it's putting it in brackets, i.e. it's formatting it as ($1,234.00) ? If so, I believe that's the intended behaviour for the US. However, you can create your own NumberFormatInfo which doesn't behave this way. Take an existing NumberFormatInfo which is "mostly right", call Clone() to make a mutable copy, and then set the