C# Declare a string that spans on multiple lines

▼魔方 西西 提交于 2019-12-04 00:29:15

问题


I'm trying to create a string that is something like this

string myStr = "CREATE TABLE myTable
(
id text,
name text
)";

But I get an error: http://i.stack.imgur.com/o6MJK.png

What is going on here?


回答1:


Make a verbatim string by prepending an at sign (@). Normal string literals can't span multiple lines.

string myStr = @"CREATE TABLE myTable
(
    id text,
    name text
)";

Note that within a verbatim string (introduced with a @) the backslash (\) is no more interpreted as an escape character. This is practical for Regular expressions and file paths

string verbatimString = @"C:\Data\MyFile.txt";
string standardString = "C:\\Data\\MyFile.txt";

The double quote must be doubled to be escaped now

string verbatimString  = @"This is a double quote ("")";
string standardString  = "This is a double quote (\")";



回答2:


string myStr = @"CREATE TABLE myTable
(
id text,
name text
)";



回答3:


Use the @ symbol infront of the string.



来源:https://stackoverflow.com/questions/12548391/c-sharp-declare-a-string-that-spans-on-multiple-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!