C# Declare a string that spans on multiple lines

前端 未结 3 1916
情书的邮戳
情书的邮戳 2021-01-11 11:26

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 err

相关标签:
3条回答
  • 2021-01-11 11:54

    Use the @ symbol infront of the string.

    0 讨论(0)
  • 2021-01-11 11:57

    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 (\")";
    
    0 讨论(0)
  • 2021-01-11 12:01
    string myStr = @"CREATE TABLE myTable
    (
    id text,
    name text
    )";
    
    0 讨论(0)
提交回复
热议问题