string-literals

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

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 affec...

C String literals: Where do they go?

I have read a lot of posts about "string literals" on SO, most of which have been about best-practices, or where the literal is NOT located in memory. I am interested in where the string DOES get allocated/stored, etc. I did find one intriguing answer here, saying: Defining a string inline actually embeds the data in the program it...

Convert javascript string to an array

I'm retrieving an array of objects from a hidden html input field. The string I'm getting is: "{"id":"1234","name":"john smith","email":"[email protected]"},{"id":"4431","name":"marry doe","email":"[email protected]"}" Now I need to pass this as an array of objects again. How do I convert this string into array of objects? ...

How safe and reliable are C++ String Literals?

So, I'm wanting to get a better grasp on how string literals in C++ work. I'm mostly concerned with situations where you're assigning the address of a string literal to a pointer, and passing it around. For example: char* advice = "Don't stick your hands in the toaster."; Now lets say I just pass this string around by copying pointer...

Why is passing a string literal into a char* argument only sometimes a compiler error?

I'm working in a C, and C++ program. We used to be compiling without the make-strings-writable option. But that was getting a bunch of warnings, so I turned it off. Then I got a whole bunch of errors of the form "Cannot convert const char* to char* in argmuent 3 of function foo". So, I went through and made a whole lot of changes to fix...

How to create a literal XML string with Objective-C?

I have long XML strings that I'm hard-coding into an iPhone project's unit tests. It's pretty ugly having to escape all the quotes and line breaks -- for example: NSString *xml = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\ <root>\ <element name=\"foo\" />\ </root>"; It would be really nice to have a lower-friction way to do th...

Weird gcc error stray/missing terminating " character in C

I get the following errors: error: missing terminating " character and error: stray `\' in program In this line of C code: system("sqlite3 -html /home/user/.rtcom-eventlogger/el.db \"SELECT service_id, event_type_id,free_text, remote_uid FROM Events WHERE remote_uid=\'%d\' ORDER BY start_time DESC;\" > lol.html", nr); "nr" is a...

Properly match a Java string literal

Hi, I am looking for a Regular expression to match string literals in Java source code. Is it possible? private String Foo = "A potato"; private String Bar = "A \"car\""; My intent is to replace all strings within another string with something else. Using: String A = "I went to the store to buy a \"coke\""; String B = A.replaceAll(...

Erlang - Eccentricity with accented characters and string literal

Hey, I am trying to implement a function to differentiate between french vowels and consonnants. It should be trivial, let's see what I wrote down : -define(vowels,"aeiouyàâéèêëôù"). is_vowel(Char) -> C = string:to_lower(Char), lists:member(C,?vowels). It's pretty simple, but it behaves incorrectly : 2> char:is_vow...

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

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 t...

Get version info of a patch file in c#

I am uploading a .msi file using fileupload control to a central location. Now i need to get version info of this file. I am using the following code. FileVersionInfo patchFile = FileVersionInfo.GetVersionInfo(completeFilePath) completeFilePath is the full path of the uploaded file. This code breaks and throws file not found exception...

C++ template argument inference and string literals

I have a "set" data type: template <class V> struct Set { void add(const V& value) {} }; I want to write a top-level function version of Set::add. template <class V> void add(const Set<V>& set, const V& value) {} This doesn't quite work with string literals: Set<const char*> set; const char* val = "a"; set.add(val); // ok set.a...

Implementation of string literal concatenation in C and C++

AFAIK, this question applies equally to C and C++ Step 6 of the "translation phases" specified in the C standard (5.1.1.2 in the draft C99 standard) states that adjacent string literals have to be concatenated into a single literal. I.e. printf("helloworld.c" ": %d: Hello " "world\n", 10); Is equivalent (syntactically) to: ...

std::string vs string literal for functions

I was wondering, I normally use std::string for my code, but when you are passing a string in a parameter for a simply comparison, is it better to just use a literal? Consider this function: bool Message::hasTag(string tag) { for(Uint tagIndex = 0; tagIndex < m_tags.size();tagIndex++) { if(m_tags[tagIndex] == tag) ...

Best way to convert string to array of object in javascript?

Hi guys, I want to convert below string to an array in javascript. {a:12, b:c, foo:bar} How do I convert this string into array of objects? Any cool idea? ...

Markdown within yaml / yaml multi-line escape sequence?

Is it possible to store unescaped markdown documents in yaml? I've tested key:|+ markdown text block that could have any combination of line breaks, >, -, :, ', " etc etc. This does not work. I need something like CDATA or python style triple-quotes for yamal. Any ideas? ...

char four[4] = "four"; What are the correct semantics for this statement?

int main(void) { char four[4] = "four"; return 0; } When compiled as a C++ program, G++ reports xxx.cpp: In function int main(): xxx.cpp:3: error: initializer-string for array of chars is too long When compiled a a C program, GCC reports no error. It appears to me, that the assignment is correctly copying all 4 bytes into t...

Why multiline string have such strange syntax? ( when they do )

Probably is something really simple that I'm missing, but what's wrong with having a string going in multiple lines? For instance, Ruby is: text = <<END Some text END And Python is: text = """ Some text """ And C# is: string text = @" Some Text"; Which come closer, but still needs the @ char...

MySQL unicode literals

I want to insert a record into MySQL that has a non-ASCII Unicode character, but I'm on a terminal that doesn't let me easily type non-ASCII characters. How do I escape a Unicode literal in MySQL's SQL syntax? ...

Checking For Equal Instances of 2 Different (Included Example)

I use the == in the code below and prints out "Equals!", why? Can someone explain why these two different strings a and b are equal? public class test { public static void main() { String a = "boy"; String b = "boy"; if(a == b) { System.out.println("Equals!"); } else ...