How to escape a semicolon in C++ string
std:string str("text1\; text2\;");
How come 开发者_开发问答VS2005 says ; unrecognized character escape sequence.
Please advise, thanks.
Because this is wrong:
std:string str("text1\; text2\;");
This is correct:
std::string str("text1; text2;");
TWO colons after std.
There is no need to escape semicolons
Just put the semicolon with no backslash:
std::string str("text1; text2;");
Semicolons have absolutely no significance in C strings; they're just normal characters there. If you need to put a backslash in the string because something later requires it, it's the backslash that needs the backslash in front.
std::string str("text1\\; text2\\;");
That's because \;
is not a recognized escape sequence in C++; the compiler rightly wants to know what on earth you're talking about when you put that in.
精彩评论