簡體   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