string-literals

What is the difference between these two ways of creating NSStrings?

独自空忆成欢 提交于 2020-01-13 17:11:25
问题 NSString *myString = @"Hello"; NSString *myString = [NSString stringWithString:@"Hello"]; I understand that using method (1) creates a pointer to a string literal that is defined as static memory (and cannot be deallocated) and that using (2) creates an NSString object that will be autoreleased. Is using method (1) bad? What are the major differences? Is there any instances where you would want to use (1)? Is there a performance difference? P.S. I have searched extensively on Stack Overflow

What's the advantage of having multi-line & single-line string literals in python?

隐身守侯 提交于 2020-01-11 10:24:27
问题 I know the triple quote strings are used as docstrings, but is there a real need to have two string literals? Are there any use case when identifying between single-line & multi-line is useful. in Clojure we have 1 string literal, is multi-line and we use it as docstring. So why the difference in python? 回答1: The advantage of having to be explicit about creating a multi-line string literal is probably best demonstrated with an example: with open("filename.ext) as f: for line in f: print(line

Performance of String literals vs constants for Session[…] dictionary keys

折月煮酒 提交于 2020-01-11 07:44:11
问题 Session[Constant] vs Session["String Literal"] Performance I'm retrieving user-specific data like ViewData["CartItems"] = Session["CartItems"]; with a string literal for keys on every request. Should I be using constants for this? If yes, how should I go about implementing frequently used string literals and will it significantly affect performance on a high-traffic site? Related question does not address ASP.NET MVC or Session . 回答1: The reason to use constants has to do with maintainability

using \ in a string as literal instead of an escape

不打扰是莪最后的温柔 提交于 2020-01-08 13:24:25
问题 bool stringMatch(const char *expr, const char *str) { // do something to compare *(expr+i) == '\\' // In this case it is comparing against a backslash // i is some integer } int main() { string a = "a\sb"; string b = "a b"; cout << stringMatch(a.c_str(), b.c_str()) << endl; return 1; } So the problem right now is: Xcode is not reading in the '\', when I was debugging in stringMatch function, expr appears only to be 'asb' instead of the literal a\sb'. And Xcode is spitting out an warning at

using \ in a string as literal instead of an escape

大兔子大兔子 提交于 2020-01-08 13:24:17
问题 bool stringMatch(const char *expr, const char *str) { // do something to compare *(expr+i) == '\\' // In this case it is comparing against a backslash // i is some integer } int main() { string a = "a\sb"; string b = "a b"; cout << stringMatch(a.c_str(), b.c_str()) << endl; return 1; } So the problem right now is: Xcode is not reading in the '\', when I was debugging in stringMatch function, expr appears only to be 'asb' instead of the literal a\sb'. And Xcode is spitting out an warning at

C++ multiline string literal

与世无争的帅哥 提交于 2020-01-08 11:21:34
问题 Is there any way to have multi-line plain-text, constant literals in C++, à la Perl? Maybe some parsing trick with #include ing a file? I can't think of one, but boy, that would be nice. I know it'll be in C++0x. 回答1: Well ... Sort of. The easiest is to just use the fact that adjacent string literals are concatenated by the compiler: const char *text = "This text is pretty long, but will be " "concatenated into just a single string. " "The disadvantage is that you have to quote " "each part,

comparing two strings with 'is' — not performing as expected

此生再无相见时 提交于 2020-01-05 20:29:33
问题 I'm attempting to compare two strings with is . One string is returned by a function, and the other is just declared in the comparison. is tests for object identity, but according to this page, it also works with two identical strings because of Python's memory optimization. But, the following doesn't work: def uSplit(ustring): #return user minus host return ustring.split('!',1)[0] user = uSplit('theuser!host') print type(user) print user if user is 'theuser': print 'ok' else: print 'failed'

Where does string-literal begin and end?

六眼飞鱼酱① 提交于 2020-01-05 07:51:39
问题 The C99 standard says the implementation limit for characters of a string literal is 4095(?). But where exactly does a literal end and begin? printf( "First part" "second part!\r\n" ); Would this be a single string literal? Or are this 2 string literals? 回答1: N1256 5.2.4.1 says: 4095 characters in a character string literal or wide string literal (after concatenation) The "after concatenation" refers to the concatenation of adjacent string literals that occurs in translation phase 6 (5.1.1.2)

“Forwarding” string literals

穿精又带淫゛_ 提交于 2020-01-04 05:06:06
问题 I am dealing with a library that has a variadic macro meant to be used like printf #define PRINTF_LIKE (FORMAT, ...) //Some statement expression Since PRINTF_LIKE was required to evaluate to something, and in order avoid the usual if and dangling else issue with macros having multiple statements, it was implemented using gcc's statement expressions. However, I need my code to build with the intel compiler, which doesn't allow destructible entities inside a statement expression. This means I

Crash when handling char * init'd with string literal, but not with malloc

左心房为你撑大大i 提交于 2020-01-04 05:04:36
问题 I was reading a book on C today, and it mentioned that the following was true; I was so curious as to why that I made this program to verify; and then ultimately post it here so someone smarter than me can teach me why these two cases are different at runtime. The specifics of the question related to the difference at runtime between how a (char *) is handled based on whether it is pointing to a string created as a literal vs. created with malloc and manual population. why is the memory