Noodlejay BlaBla

welcome and happy to share


發表留言

C# Verbatim String start with @ 字串使用

C# String類別,相比於char[]大幅度提升了處理字串的方便性,當然也不是char[]就毫無用處,但不是本篇的重點,就不多說明。

Verbatim string literals

在字串的前面加入@符號,後面雙引號內的內容,就會直接是輸出的內容,不會考慮跳脫字元

string str=@"c:\Docs\Source\a.txt";  

string str2="c:\\Docs\\Source\\a.txt";  // Rather than c:\\Docs\\Source\\a.txt

Console.WriteLine(str);
Console.WriteLine(str2);

輸出結果

c:\Docs\Source\a.txt
c:\Docs\Source\a.txt

由於反斜線 \ ,屬於跳脫字元需要用 “\\",才能正確輸出

參考文章:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/reference-types

https://ithelp.ithome.com.tw/articles/10213867