繁体   English   中英

字符串文字C ++?

[英]String literals C++?

我需要在C ++中执行以下C#代码(如果可能的话)。 我必须使用很多引人注目的引号和其他内容来构造一个长字符串。

const String _literal = @"I can use "quotes" inside here";

这在C ++ 03(当前标准)中不可用。

这是C ++ 0x草案标准的一部分,但目前尚不可用。

现在,您只需要明确引用它:

const std::string _literal = "I have to escape my quotes in \"C++03\"";

一旦C ++ 0x成为现实,你就可以写:

const std::string _literal = R"(but "C++0x" has raw string literals)";

当你需要)"在你的文字中:

const std::string _literal = R"DELIM(more "(raw string)" fun)DELIM";

在C ++中没有C#的“@”。 实现它的唯一方法是正确地转义字符串:

const char *_literal = "I can use \"quotes\" inside here";

C ++中没有原始字符串文字。 你需要转义你的字符串文字。

std::string str = "I can use \"quotes\" inside here";

C ++ 0x在可用时提供原始字符串文字:

R"C:\mypath"

顺便说一下,你不应该用前导下划线命名任何东西,因为这样的标识符在C ++中保留。

在C ++中没有这样的机制。 你必须采用老式的方式,通过使用逃生。

但是,您可以使用脚本语言使转义部分更容易一些。 例如,Ruby中的%Q运算符将在irb使用时返回正确转义的双引号字符串:

irb(main):003:0> %Q{hello "world" and stuff     with    tabs}
=> "hello \"world\" and stuff\twith\ttabs"

暂无
暂无

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

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