C#声明跨多行的字符串 [英] C# Declare a string that spans on multiple lines

查看:600
本文介绍了C#声明跨多行的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个类似这样的字符串

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

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

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

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

这是怎么回事?

推荐答案

通过在符号前加一个 (@)来制作逐字字符串.普通的字符串文字不能跨越多行.

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

这篇关于C#声明跨多行的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆