C# 声明一个跨越多行的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12548391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:37:22  来源:igfitidea点击:

C# Declare a string that spans on multiple lines

c#stringnewlinedeclare

提问by Ben

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

但我收到一个错误:http: //i.stack.imgur.com/o6MJK.png

What is going on here?

这里发生了什么?

采纳答案by Olivier Jacot-Descombes

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 expressionsand file paths

请注意,在逐字字符串(用 a 引入@)中,反斜杠 ( \) 不再被解释为转义字符。这对于正则表达式和文件路径很实用

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 (\")";

回答by Ozerich

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

回答by Petur Subev

Use the @ symbol infront of the string.

使用字符串前面的@ 符号。