简体   繁体   English

如何在C ++字符串中转义分号

[英]How to escape a semicolon in C++ string

std:string str("text1\; text2\;");

How come VS2005 says ; unrecognized character escape sequence. 为什么VS2005说; unrecognized character escape sequence. ; 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. 标准后的两个冒号。

没有必要逃脱分号

只需将分号放在没有反斜杠的情况下:

std::string str("text1; text2;");

Semicolons have absolutely no significance in C strings; 分号在C字符串中绝对没有意义; 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++; 不是C ++中公认的转义序列; the compiler rightly wants to know what on earth you're talking about when you put that in. 当你把它放进去时,编译器正确地想知道你在说什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM