string-formatting

WPF Image Tooltip

不打扰是莪最后的温柔 提交于 2019-11-30 05:41:14
问题 I have a tooltip on an image inside of a listbox. The tooltip is setup as follows: <Image Grid.Column="0" Source="{Binding PingRankImage}" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Center"> <Image.ToolTip> <ToolTip Content="{Binding Ping, StringFormat='Ping: {0}ms'}" ContentStringFormat="{}Ping: {0}ms}" /> </Image.ToolTip> </Image> but the tooltip just displays the value and not the 'Ping: XXXms' Any ideas? 回答1: You don't need extra {} prefix in

str.format() raises KeyError

亡梦爱人 提交于 2019-11-30 05:33:48
The following code raises a KeyError exception: addr_list_formatted = [] addr_list_idx = 0 for addr in addr_list: # addr_list is a list addr_list_idx = addr_list_idx + 1 addr_list_formatted.append(""" "{0}" { "gamedir" "str" "address" "{1}" } """.format(addr_list_idx, addr)) Why? I am using Python 3.1. Lasse Vågsæther Karlsen The problem is those { and } characters you have there that don't specify a key for formatting. You need to double them up, so change your code to: addr_list_formatted.append(""" "{0}" {{ "gamedir" "str" "address" "{1}" }} """.format(addr_list_idx, addr)) 来源: https:/

Format number with + and - sign

浪子不回头ぞ 提交于 2019-11-30 05:24:58
问题 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

php sprintf format string

我怕爱的太早我们不能终老 提交于 2019-11-30 04:53:43
问题 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 回答1: 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. 回答2: This is called "Argument swapping" and documented in example #3 here: http://php.net/sprintf

Using locals() and format() method for strings: are there any caveats?

醉酒当歌 提交于 2019-11-30 04:11:42
Are there any disadvantages, caveats or bad practice warnings about using the following pattern? def buildString(user, name = 'john', age=22): userId = user.getUserId() return "Name: {name}, age: {age}, userid:{userId}".format(**locals()) I had a very repetitive string generation code to write and was tempted to use this, but something about using locals() makes me uncomfortable. Is there any danger of unexpected behavior in this? Edit: context I found myself constantly writing stuff like: "{name} {age} {userId} {etc}...".format(name=name, age=age, userId=userId, etc=etc) There is now an

Is there a C# function that formats a 64bit “Unsigned” value to its equivalent binary value?

妖精的绣舞 提交于 2019-11-30 01:19:17
问题 To format/display a number to its equivalent binary form (in C#), I have always simply called: Convert.ToString(myNumber, 2); Today, I just realized that the .ToString() overload that I have been calling does not support values that are greater than 9223372036854775807. Note the .ToString() overload's signature is: .ToString(long, int) . Where "long" is a 64bit signed value which max's out at 9223372036854775807. To put it another way, using C#, when I run this: Convert.ToString

string.format() with optional placeholders

寵の児 提交于 2019-11-30 00:49:25
问题 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

How to provide custom string placeholder for string format

你离开我真会死。 提交于 2019-11-29 22:59:39
I have a string string str ="Enter {0} patient name"; I am using string.format to format it. String.Format(str, "Hello"); Now if i want patient also to be retrieved from some config then I need to change str to something like "Enter {0} {1} name" . So it will replace the {1} with second value. The problem is that I want instead of {1} some other format something like {pat} . But when I try to use, it throws an error. The reason I want a different format is that there are lot of files I need to change like this(which may contain {0},{1} etc). So I need a custom placeholder which can be replaced

Formatting dict keys: AttributeError: 'dict' object has no attribute 'keys()'

旧街凉风 提交于 2019-11-29 21:36:06
问题 What is the proper way to format dict keys in string? When I do this: >>> foo = {'one key': 'one value', 'second key': 'second value'} >>> "In the middle of a string: {foo.keys()}".format(**locals()) What I expect: "In the middle of a string: ['one key', 'second key']" What I get: Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> "In the middle of a string: {foo.keys()}".format(**locals()) AttributeError: 'dict' object has no attribute 'keys()' But as you can see, my

How can I format a decimal bound to TextBox without angering my users?

眉间皱痕 提交于 2019-11-29 21:14:39
I'm trying to display a formatted decimal in a TextBox using data binding in WPF. Goals Goal 1: When setting a decimal property in code, display 2 decimal places in the TextBox. Goal 2: When a user interacts with (types in) the TextBox, don't piss him/her off. Goal 3: Bindings must update source on PropertyChanged. Attempts Attempt 1: No formatting. Here we're starting nearly from scratch. <TextBox Text="{Binding Path=SomeDecimal, UpdateSourceTrigger=PropertyChanged}" /> Violates Goal 1. SomeDecimal = 4.5 will show "4.50000" in the TextBox. Attempt 2: Use StringFormat in the Binding. <TextBox