string-formatting

Output single character in C

若如初见. 提交于 2019-11-27 21:02:02
When printing a single character in a C program, must I use "%1s" in the format string? Can I use something like "%c"? Evan Teran yes, %c will print a single char: printf("%c", 'h'); also, putchar / putc will work too. From "man putchar": #include <stdio.h> int fputc(int c, FILE *stream); int putc(int c, FILE *stream); int putchar(int c); * fputc() writes the character c, cast to an unsigned char, to stream. * putc() is equivalent to fputc() except that it may be implemented as a macro which evaluates stream more than once. * putchar(c); is equivalent to putc(c,stdout). EDIT: Also note, that

Python - convert list of tuples to string

╄→гoц情女王★ 提交于 2019-11-27 20:39:15
Which is the most pythonic way to convert a list of tuples to string? I have: [(1,2), (3,4)] and I want: "(1,2), (3,4)" My solution to this has been: l=[(1,2),(3,4)] s="" for t in l: s += "(%s,%s)," % t s = s[:-1] Is there a more pythonic way to do this? you might want to use something such simple as: >>> l = [(1,2), (3,4)] >>> str(l).strip('[]') '(1, 2), (3, 4)' .. which is handy, but not guaranteed to work correctly You can try something like this ( see also on ideone.com ): myList = [(1,2),(3,4)] print ",".join("(%s,%s)" % tup for tup in myList) # (1,2),(3,4) How about: >>> tups = [(1, 2),

String formatting options: pros and cons

杀马特。学长 韩版系。学妹 提交于 2019-11-27 20:06:44
These are two very popular ways of formatting a string in Python. One is using a dict : >>> 'I will be %(years)i on %(month)s %(day)i' % {'years': 21, 'month': 'January', 'day': 23} 'I will be 21 on January 23' And the other one using a simple tuple : >>> 'I will be %i on %s %i' % (21, 'January', 23) 'I will be 21 on January 23' The first one is way more readable, but the second one is faster to write. I actually use them indistinctly. What are the pros and cons of each one? regarding performance, readability, code optimization (is one of them transformed to the other?) and anything else you

String.Format alternative in C++ [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-11-27 19:42:55
This question already has an answer here: std::string formatting like sprintf 39 answers I don't have much experience working with C++. Rather I have worked more in C# and so, I wanted to ask my question by relating to what I would have done in there. I have to generate a specific format of the string, which I have to pass to another function. In C#, I would have easily generated the string through the below simple code. string a = "test"; string b = "text.txt"; string c = "text1.txt"; String.Format("{0} {1} > {2}", a, b, c); By generating such an above string, I should be able to pass this in

Formattting Phone number in Swift

梦想的初衷 提交于 2019-11-27 18:24:36
I'm formatting my textfiled text once the user start typing the phone number into this format type 0 (555) 444 66 77 and it is working fine but once I get the number from the server I get it like this 05554446677 So please could you tell me how I can edit it in the same format once I get it fro the server? My code once I start typing: func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if textField == phoneNumberTextField{ var newString = (textField.text as NSString).stringByReplacingCharactersInRange(range,

How to create a NSString from a format string like @“xxx=%@, yyy=%@” and a NSArray of objects?

五迷三道 提交于 2019-11-27 18:00:37
Is there any way to create a new NSString from a format string like @"xxx=%@, yyy=%@" and a NSArray of objects? In the NSSTring class there are many methods like: - (id)initWithFormat:(NSString *)format arguments:(va_list)argList - (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList + (id)stringWithFormat:(NSString *)format, ... but non of them takes a NSArray as an argument, and I cannot find a way to create a va_list from a NSArray... Peter N Lewis It is actually not hard to create a va_list from an NSArray. See Matt Gallagher's excellent article on the subject

Better String formatting in Scala

こ雲淡風輕ζ 提交于 2019-11-27 17:57:18
With too many arguments, String.format easily gets too confusing. Is there a more powerful way to format a String. Like so: "This is #{number} string".format("number" -> 1) Or is this not possible because of type issues ( format would need to take a Map[String, Any], I assume; don’t know if this would make things worse). Or is the better way doing it like this: val number = 1 <plain>This is { number } string</plain> text even though it pollutes the name space? Edit: While a simple pimping might do in many cases, I’m also looking for something going in the same direction as Python’s format()

C++ printf with %f but localized for the user's country

给你一囗甜甜゛ 提交于 2019-11-27 17:36:18
问题 I'm using the following C++ syntax to output a floating point value on a Windows platform: printf("%.2f", 1.5); It works well if I run it on an English user account. My assumption was that if I run it on, say French user account, the output will be 1,50 instead of 1.50. Why do I not see it and how to produce my desired result? 回答1: The radix character (i.e. '.' or ',') is defined by the current locale. The default locale (at least for Windows systems) is "C", which defines '.' as radix

PHP: How to add leading zeros/zero padding to float via sprintf()?

会有一股神秘感。 提交于 2019-11-27 17:31:29
问题 I'm using sprintf() to get a formatted string of some float numbers with a certain precision. In addition, I wanted to add leading zeros to make all numbers even in length. Doing that for integers is pretty straight forward: sprintf('%02d', 1); This will result in 01 . However, trying the same for a float with precision doesn't work: sprintf('%02.2f', 1); Yields 1.00 . How can I add leading zeros to a float value? 回答1: Short answer: sprintf('%05.2f', 1); will give the desired result 01.00

How can CString be passed to format string %s?

 ̄綄美尐妖づ 提交于 2019-11-27 16:01:36
class MyString { public: MyString(const std::wstring& s2) { s = s2; } operator LPCWSTR() const { return s.c_str(); } private: std::wstring s; }; int _tmain(int argc, _TCHAR* argv[]) { MyString s = L"MyString"; CStringW cstring = L"CString"; wprintf(L"%s\n", (LPCWSTR)cstring); // Okay. Becase it has an operator LPCWSTR() wprintf(L"%s\n", cstring); // Okay, fine. But how? wprintf(L"%s\n", (LPCWSTR)s); // Okay. fine. wprintf(L"%s\n", s); // Doesn't work. Why? It prints gabage string like "?." return 0; } How can CString be passed to format string %s? By the way, MSDN says (it's weird) To use a