【问题标题】:C# Declare a string that spans on multiple linesC# 声明一个跨越多行的字符串
【发布时间】:2012-09-14 22:20:02
【问题描述】:

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

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

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

这是怎么回事?

【问题讨论】:

    标签: c# string newline declare


    【解决方案1】:

    通过在 at 符号之前添加一个逐字字符串 (@)。普通字符串文字不能跨越多行。

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

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

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

    双引号现在必须加倍才能转义

    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】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-04-14
          • 2016-11-14
          • 1970-01-01
          • 2010-12-06
          • 1970-01-01
          • 2021-09-26
          • 1970-01-01
          相关资源
          最近更新 更多