string-formatting

C - Format char array like printf

試著忘記壹切 提交于 2019-12-21 05:13:11
问题 I want to format a c string like printf does. For example: char string[] = "Your Number:%i"; int number = 33; // String should now be "Your Number:33" Is there any library or a good way I could do this? 回答1: int main() { char str[] = "Your Number:%d"; char str2[1000]; int number = 33; sprintf(str2,str,number); printf("%s\n",str2); return 0; } Output: ---------- Capture Output ---------- > "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe Your Number:33 > Terminated with exit code 0. 回答2:

Is there a difference between `%`-format operator and `str.format()` in python regarding unicode and utf-8 encoding?

£可爱£侵袭症+ 提交于 2019-12-21 04:39:06
问题 Assume that n = u"Tübingen" repr(n) # `T\xfcbingen` # Unicode i = 1 # integer The first of the following files throws UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 82: ordinal not in range(128) When I do n.encode('utf8') it works. The second works flawless in both cases. # Python File 1 # #!/usr/bin/env python -B # encoding: utf-8 print '{id}, {name}'.format(id=i, name=n) # Python File 2 # #!/usr/bin/env python -B # encoding: utf-8 print '%i, %s'% (i, n) Since

How do I format a string with properties from a bean

邮差的信 提交于 2019-12-21 02:26:57
问题 I want to create a String using a format, replacing some tokens in the format with properties from a bean. Is there a library that supports this or am I going to have to create my own implementation? Let me demonstate with an example. Say I have a bean Person ; public class Person { private String id; private String name; private String age; //getters and setters } I want to be able to specify format strings something like; "{name} is {age} years old." "Person id {id} is called {name}." and

What is the difference between %i and %d in Python? [duplicate]

空扰寡人 提交于 2019-12-20 20:02:21
问题 This question already has answers here : What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf) (4 answers) Closed 6 years ago . OK I was looking into number formatting and I found that you could use %d or %i to format an integer. For example: number = 8 print "your number is %i." % number or number = 8 print "your number is %d." % number But what is the difference? I mean i found something but it was total jibberish. Anyone speak Mild-Code

Decimal stores precision from parsed string in C#? What are the implications?

心不动则不痛 提交于 2019-12-20 17:42:37
问题 During a conversation on IRC, someone pointed out the following: decimal.Parse("1.0000").ToString() // 1.0000 decimal.Parse("1.00").ToString() // 1.00 How/why does the decimal type retain precision (or, rather, significant figures) like this? I was under the impression that the two values are equal, not distinct. This also raises further questions: How is the number of significant figures decided during mathematical operations? Does the number of significant figures get retained during

Should we store format strings in resources?

自闭症网瘾萝莉.ら 提交于 2019-12-20 17:39:27
问题 For the project that I'm currently on, I have to deliver specially formatted strings to a 3rd party service for processing. And so I'm building up the strings like so: string someString = string.Format("{0}{1}{2}: Some message. Some percentage: {3}%", token1, token2, token3, number); Rather then hardcode the string, I was thinking of moving it into the project resources: string someString = string.Format(Properties.Resources.SomeString, token1, token2, token3, number); The second option is in

Check for integer in string array

寵の児 提交于 2019-12-20 07:32:06
问题 I am trying to check a string array for existence of a converted integer number. This sits inside of a procedure where: nc_ecosite is an integer variable current_consite is a string array ecosite is an integer current_ecosite_nc is double IF to_char(nc_ecosite, '999') IN (select current_consite from current_site_record where current_ecosite_nc::integer = nc_ecosite) THEN ecosite := nc_ecosite; The result always comes from the ELSIF that follows the first IF . This occurs when nc_ecosite is in

Check for integer in string array

不问归期 提交于 2019-12-20 07:31:23
问题 I am trying to check a string array for existence of a converted integer number. This sits inside of a procedure where: nc_ecosite is an integer variable current_consite is a string array ecosite is an integer current_ecosite_nc is double IF to_char(nc_ecosite, '999') IN (select current_consite from current_site_record where current_ecosite_nc::integer = nc_ecosite) THEN ecosite := nc_ecosite; The result always comes from the ELSIF that follows the first IF . This occurs when nc_ecosite is in

StringFormat of Datetime object in binding gives back 0 for hour and minute

北战南征 提交于 2019-12-20 06:12:27
问题 I create a Datetime object with Datetime.Now and I have this as a property of a class. When I bind this to a grid view: <GridViewColumn Header="Date" Width="120" DisplayMemberBinding="{Binding transaction_date1, StringFormat=HH:mm}" /> The result is always 00:00. When I debug the code I see that the hour, minute etc. properties of the Datetime object contain non-zero values. I think that the StringFormat in this case gets the hour and minute from the Date object within the Datetime object

How to replace strings in StringFormat in WPF Binding

穿精又带淫゛_ 提交于 2019-12-20 05:55:58
问题 I need to replace a , with ,\n (New Line) in my string i want to do it on ClientSide in StringFormat <TextBlock Grid.Row="0" Text="{Binding Address}" Grid.RowSpan="3" /> How can i do this? 回答1: You can't do this via a StringFormat binding operation, as that doesn't support replacement, only composition of inputs. You really have two options - expose a new property on your VM that has the replaced value, and bind to that, or use an IValueConverter to handle the replacement. A value converter