C # gives strings literals--string insertions, use of escape sequences

Source: Internet
Author: User

1. Placeholder, string insertion

When a string is given a literal value, it is often encountered in the case of a string containing variables, splicing with the connector, the way of conversion is more troublesome, but also error-prone. C # provides a more convenient way to handle the ' placeholder ', as well as the new feature of c#6 ' Insert String '(VS2015 and subsequent new versions are available for this function). Here's a simple example of how to use:

String A= "a friend in Need";

String b= "a friend indeed.";

Want to output a friend in need is a friend indeed. This sentence can be selected as follows:

(1) Placeholder

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

/* If you need to use a variable inside a string, use the {number} placeholder at that location (numbering starts at 0), and then write the content in the corresponding position */

(2) inserting a string

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

/* Add $ to the front of the double quotation mark and enclose the variable in the double quotation mark with {}, and the variable within the double quotation marks can be output as the variable represents.

2. Escape sequences

The following content is reproduced from the nightclub bullying blog

Original link: literal value of a string in C # (escape sequence)

In program development, you often encounter the use of escape sequences in literals in strings, and the following table collects a complete list of the following escape sequences so that you can see the references:

Escape sequence List
Escape sequences The resulting character Unicode value of the character
\‘ Single quotation marks 0x0027
\" Double quotes 0x0022
\\ Back slash 0x005c
/ Empty 0x0000
\a Warning (beep-generated) 0x0007
\b Backspace 0x0008
\f Page change 0x000c
\ n Line break 0x000A
\ r Enter 0x000d
\ t Horizontal tab 0x0009
\v Vertical tab 0x000b

The Unicode Value column in the table is a 16 binary value of the character in the Unicode character set. Unicode escape sequences can be used to specify Unicode characters, which include the standard \ character followed by a U and a 4-bit hexadecimal value (for example, 4 digits after x in the table).

The following string is equivalent:

"Karli\ ' s string."

"Karli\u0027s string."

It is clear that Unicode escape sequences have more uses.

You can also specify a string literal, that is, all characters between two double quotation marks are included in the string, including the end of line characters and the characters that need to be escaped. The only exception is the escape of the double-quote character, which must be specified to avoid ending the string. To do this, you can add an @ character before the string:

@ "verbatim string literal."

You can specify this string in a general way, but you need to use this method:

@ "A short list:

Item 1

Item2 "

Verbatim-specified strings are useful in file names because the backslash characters are used extensively in filenames. If you use a generic string, you must use two backslashes in the string, for example:

"C:\\temp\\mydir\\myfile.doc"

With literal string literals, this code is easier to read. The following string is equivalent to the above:

The characters after the @ "C:\Temp\MyDir\MyFile.doc" //@ are interpreted literally (as if \ In the statement is no longer considered an escape sequence, but rather as a character)

C # gives strings literals--string insertions, use of escape sequences

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.