escape sentence and VerBatim literal

當字串中包含一些已有特殊意義的符號時,例如雙引號,雙引號已經被定義為刮在字串旁邊
,若你在雙引號裡面又加了",但你又想讓它呈現出來,像是你想顯示"hello world",
你可以有兩種選擇

1.escape sequence逃脫字元

string str ="\"hello word\"";

如果寫 string ""hello world""會報錯,所以用\符號來逃脫

以下是官方的文件

https://docs.microsoft.com/en-us/cpp/c-language/escape-sequences?view=vs-2017



2.VerBatim literal

其實第一種方法可用,但可讀性比較不好,第二種方法是在字串前面加上@符號
這樣compiler就會把這串當成都是一般的字沒特殊含義!

但雙引號比較特殊,在這用法還是得用兩個雙引號
  string s = @"""hello world""";

請參考:https://stackoverflow.com/questions/17168961/how-do-i-escape-in-verbatim-string

留言

這個網誌中的熱門文章

刪除大量資料造成Lock escalation鎖定升級 SQL Server

避免登出後,讀取快取返回上一頁。 How to prevent seeing previous page after logout?

SAP Connector for Microsoft .NET 3.0-------Cannot convert String into DATE/TIME