string-formatting

Include a variable inside a NSString?

允我心安 提交于 2019-11-30 22:19:19
This works fine, we all know that: NSString *textoutput = @"Hello"; outLabel.text = textoutput; However, what if you want to include a variable inside that NSString statement like the following: NSString *textoutput =@"Hello" Variable; In C++ I know when I cout something and I wanted to include a variable all I did was soemthing like this: cout << "Hello" << variableName << endl; So I'm trying to accomplish that with Objective-C but I don't see how. You can do some fancy formatting using the following function: NSString *textoutput = [NSString stringWithFormat:@"Hello %@", variable]; Note that

Format number with + and - sign

寵の児 提交于 2019-11-30 21:28:04
I have some WPF textblocks in a stackpanel that I want to databind and format. E.g. the following formats a date 24h style without the seconds part: <TextBlock Text="{Binding MyCustomObject, StringFormat={}{0:HH:mm}}" /> Now, I would like to bind an integer and also display the + and - sign (i.e. +6 or -4). <TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#}}" /> This however, does not work. Is this possible or do I have to write a complete converter just for this? EDIT Nikolays post led me to the answer: <TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#;-#;'

php sprintf format string

为君一笑 提交于 2019-11-30 20:44:23
i know about sprintf but how can i use the same param in any place? example sprintf("blabla %s 11111 %s", "test"); -it say few params, but i want to place 'test' in two place without duplicate Use the %$ numbered placeholder notation: sprintf('blabla %1$s 11111 %1$s', "test"); Here, both occurrences of %1$s will be replaced with "test" . There is more on this in the sprintf() manual page . This is called "Argument swapping" and documented in example #3 here: http://php.net/sprintf Use "%1$s" , to use argument 1, you can use this multiple times within your format string, as shown in Example #4

What does the dollar sign ($) do in a printf format string? [duplicate]

时光怂恿深爱的人放手 提交于 2019-11-30 20:04:25
问题 This question already has answers here : Selecting parameters in String.format() [duplicate] (3 answers) Closed 4 years ago . I am trying to format in java using the printf statement like in this webpage: Click Here. But I just can't figure out what the purpose of the $ sign is. Can someone please explain this to me? Input: java 100 cpp 65 python 50 Expected Output: ( there should be a space instead of _ ) ================================ java___________100 cpp___________065 python________

How to print “justified” text in the console using modern C++

帅比萌擦擦* 提交于 2019-11-30 19:58:00
问题 How can I format text "justified" so it aligns to the left and right side for a given width? int main() { printJustified("A long text with many words. " "A long text with many words. " "A long text with many words. " "A long text with many words. " "A long text with many words."); } Expected output: A long text with many words. A long text with many words. A long text with many words. A long text with many words. 回答1: A simple example how to solve this problem is this: #include <iostream>

Using StringFormat in a Binding to display Hex value with spaces

一曲冷凌霜 提交于 2019-11-30 19:02:42
问题 I haven't been able to figure out how to use StringFormat to display hex as: 08 A4 23 F5 Specifically I want the spaces after every 2 characters. When I do this: Text="{Binding MyIntValue, StringFormat={}{0:x}}" It looks like this: 08A423F5 My backup plan is using a converter on the binding but I would like to know if is possible to do it with just StringFormat. 回答1: Based on what the documentation says about the The hexadecimal ("X") format specifier and after having a play with Custom

string.format() with optional placeholders

我的梦境 提交于 2019-11-30 17:54:37
I have the following Python code (I'm using Python 2.7.X): my_csv = '{first},{middle},{last}' print( my_csv.format( first='John', last='Doe' ) ) I get a KeyError exception because 'middle' is not specified (this is expected). However, I want all of those placeholders to be optional. If those named parameters are not specified, I expect the placeholders to be removed. So the string printed above should be: John,,Doe Is there built in functionality to make those placeholders optional, or is some more in depth work required? If the latter, if someone could show me the most simple solution I'd

WPF XAML StringFormat: Culture Workaround broken in C# 4.0?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 17:53:08
问题 The work around... FrameworkElement.LanguageProperty.OverrideMetadata( typeof(FrameworkElement), new FrameworkPropertyMetadata( XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag))); ...used to work till now (mentioned here: StringFormat Localization issues in wpf). Instead till I ported my application from 3.5SP1 to 4.0, it was working. But now in 4.0 it stopped working again. Anybody experiencing this? EDIT: It is now not even working in 3.5SP1. I think this has something to

Get keys from template

主宰稳场 提交于 2019-11-30 17:45:40
I would like to get a list of all possible keyword arguments a string template might use in a substitution. Is there a way to do this other than re? I want to do something like this: text="$one is a $lonely $number." keys = get_keys(text) # keys = ('one', 'lonely', 'number') I'm writing a simple Mad-lib-like program, and I want to perform template substitution with either string.format or Template strings . I'd like to write the 'story' and have my program produce a template file of all the 'keywords' (nouns, verbs, etc.) that a user would need to produce. I know I can do this with regular

How to read the python string formatting grammar?

情到浓时终转凉″ 提交于 2019-11-30 17:41:31
问题 The python documentation has information on the grammar of formatting strings, however I can't seem to find information on how to read the table defining the grammar for the replacement field. replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}" field_name ::= arg_name ("." attribute_name | "[" element_index "]")* arg_name ::= [identifier | integer] attribute_name ::= identifier element_index ::= integer | index_string index_string ::= <any source character except "]"