C # two types of strings are supported: Rule string and verbatim string.

Source: Internet
Author: User

 

A rule string consists of zero or multiple characters (such as "hello") in double quotation marks and can contain simple escape sequences (such as \ t indicating a tab), hexadecimal escape sequence and Unicode escape sequence.

A verbatim string consists of the double quotation marks followed by the @ character, zero or multiple characters, and ending double quotation marks. A simple example is @ "hello ". In a verbatim string, the character between separators is interpreted word by word. The only exception is the "quotation mark escape sequence ". Specifically, simple escape sequences, hexadecimal and Unicode escape sequences are not processed in a verbatim string. A verbatim string can span multiple rows.

String-literal :( string :)
Regular-string-literal (Rule string)
Verbatim-string-literal (verbatim string)
Regular-string-literal :( rule string :)
"Regular-string-literal-charactersopt" ("Optional rule string characters ")
Regular-string-literal-characters :( rule string character :)
Regular-string-literal-character (Rule string character)
Regular-string-literal-characters regular-string-literal-character (Rule string rule string character)
Regular-string-literal-character :( rule string character :)
Single-regular-string-literal-character (single rule string character)
Simple-escape-sequence (simple escape sequence)
Hexadecimal-escape-sequence (hexadecimal escape sequence)
Unicode-escape-sequence (UNICODE escape sequence)
Single-regular-string-literal-character :( single rule string character :)
Any character except "(U + 0022), \ (U + 005c), and line break
Verbatim-string-literal :( verbatim string :)
@ "Verbatim-string-literal-charactersopt" (@ "Optional verbatim string ")
Verbatim-string-literal-characters :( verbatim string character :)
Verbatim-string-literal-character (a verbatim string)
Verbatim-string-literal-characters verbatim-string-literal-character (a verbatim string character, a verbatim string character)
Verbatim-string-literal-character :( verbatim string character :)
Single-verbatim-string-literal-character (a single verbatim string)
Quote-escape-sequence (Quotation Mark escape sequence)
Single-verbatim-string-literal-character :( a single verbatim string character :)
Any character"
Quote-escape-sequence :( quotes escape sequence :)
""
The character following the backslash (\) in the Rule string must be one of the following: ', ", \, 0, A, B, F, N, R, t, u, u, X, V. Otherwise, a compilation error occurs.

Example

String A = "Hello, world"; // Hello, world
String B = @ "Hello, world"; // Hello, world
String c = "Hello \ t world"; // Hello World
String d = @ "Hello \ t world"; // Hello \ t world
String E = "Joe said \" Hello \ "to me"; // Joe said "hello" to me
String F = @ "Joe said" "Hello" "to me"; // Joe said "hello" to me
String G = "\\\\\ Server \ share \ file.txt"; // \\ Server \ share \ file.txt
String H = @ "\ Server \ share \ file.txt"; // \ Server \ share \ file.txt
String I = "one \ r \ ntwo \ r \ nthree ";
String J = @ "One
Two
Three "; displays various strings. The last string J is a verbatim string that spans multiple rows. Characters between quotation marks (including blank spaces, such as line breaks) are also reserved by character.

Because the hexadecimal escape sequence can contain a variable number of hexadecimal numbers, the string "\ x123" contains only one character with a hexadecimal value of 123. To create a character that contains a hexadecimal value of 12 followed by a character string of 3, you can rewrite it to "\ x00123" or "\ X12" + "3 ".

The "string" type is string.

Each string does not necessarily generate a new string instance. When two or more strings are confirmed to be equal according to the character string equality operator (section 7.9.7 ),ProgramWhen the set, these strings reference the same string instance. For example,

Class Test
{
Static void main (){
Object A = "hello ";
Object B = "hello ";
System. Console. writeline (A = B );
}
} The output is true because the two strings reference the same string instance.

Reprinted: http://www.caodong.net/Article/703.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.