C # assign the literal value to a string-Use of string insertion and escape sequences,

Source: Internet
Author: User

C # assign the literal value to a string-Use of string insertion and escape sequences,
1. placeholder, string insertion

When a character string is given a literal value, it is often used to include variables in the string. It is difficult to concatenate and convert strings with connectors and is prone to errors. C # provides a convenient processing method, that is, 'placeholders ', andC #6 new feature 'insert string'(If you remember correctly, this function is available in VS2015 and later versions ). The following is a simple example to illustrate the usage:

String a = "A friend in need ";

String B = "a friend indeed .";

To output A friend in need is a friend indeed. You can select the following method:

(1) placeholder

Console. WriteLine ("{0} is {1}", A friend in need, a friend indeed .");

/* When variables are needed in the string, use the {number} placeholder (number starts from 0) at this position, and then write the content in the corresponding position */

(2) Insert a string

Console. WriteLine ($ "{a} is {B }");

/* Add $ before double quotation marks, and enclose the variables in double quotation marks with {}. Variables in double quotation marks can be output according to the content represented by variables */

2. Escape Sequence

The following content:ReprintedSelf-club and rogue blogs

Original article: The Literal Value of a string in C # (escape sequence)

During program development, we often encounter the use of escape sequences in the literal values of strings. The following table lists the complete escape sequences for reference:

Escape Sequence List
Escape Sequence Generated characters Unicode value of a character
\' Single quotes Zero x 0027
\" Double quotation marks Zero x 0022
\\ Backslash 0x005C
\ 0 Null Zero x 0000
\ Warning (generate beep) Zero x 0007
\ B Return Zero x 0008
\ F Form feed 0x000C
\ N Line feed 0x000A
\ R Enter 0x000D
\ T Horizontal Tab Zero x 0009
\ V Vertical Tab 0x000B

The "Unicode value" column in the table is the hexadecimal value of the character in the Unicode Character Set. You can use Unicode escape sequences to specify Unicode characters. The Escape sequences contain standard \ characters, followed by a u and a 4-digit hexadecimal value (for example, 4 digits after x in the table ).

The following strings are equivalent:

"Karli \'s string ."

"Karli \ u0027s string ."

Obviously, Unicode escape sequences are more useful.

You can also specify a string literally, that is, all characters between two double quotes are included in the string, including the character at the end of the line and the character to be escaped. The only exception is the escape of double quotation marks. They must be specified to avoid ending the string. Therefore, you can add a @ character before the string:

@ "Verbatim string literal ."

You can specify this string in general mode, but you need to use the following method:

@ "A short list:

Item 1

Item2"

The character string specified by word is very useful in the file name because a large number of backslash characters are used in the file name. If a common string is used, two backslashes must be used in the string. For example:

"C: \ Temp \ MyDir \ MyFile.doc"

This code is easier to read with the string literal value specified by words. The following string is equivalent to the above one:

@ "C: \ Temp \ MyDir \ MyFile.doc "//@ All subsequent characters are interpreted literally(For example, "\" in this statement is not considered as an escape sequence, but as a character)

Related Article

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.