Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the literal amount of C # Grammar text

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "sample analysis of the literal amount of C# grammatical text", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "sample analysis of the literal amount of C# grammatical text".

Literal amount of C # grammar text

In daily life, text is used to represent content other than numbers. For example, there is a man named "Bill" whose position is "section chief". Then, both "Bill" and "Section Chief" can be called texts. In computers, real-world text is often called characters and strings. For example, every Chinese character in Bill is a character. And the whole makes up a string. The same is true of the section chief.

Writing specifications for characters and strings

What we see above is the common literal amount of text. However, in many cases, digital content does not necessarily express numerical values. For example, Bill's phone number is 13466668888. For "13466668888", we will not understand it as 13.466 billion 6668888, but as a telephone number. Due to the limited ability of the computer, in order to avoid this kind of information causing trouble to the computer, the program has the following provisions on the representation of characters and strings:

A character is a single letter, number, word, or symbol enclosed in single quotes (').

A string can represent a sequence of characters.

The string uses a pair of double quotation marks (") to combine with multiple characters.

There can be any number of characters in double quotation marks of a string.

According to this specification, if you want to represent the letter an in the program, it needs to be written as'a'. A character can contain only one character, so the character'a' is 1 in length. If you want to represent Bill's phone number in the program, it must be written as "13466668888". The length of the string is 11 because it contains 11 characters, and double quotes are string format symbols and cannot be counted as string lengths.

Special characters and strings-escape characters

Sometimes you need to use special characters in a string, such as the string abc "s. The string" abc "s" in the code produces an error and the compiler cannot recognize the specific contents of the string. To solve this problem, you need to use escape characters. Escape characters represent some special characters by combining a backslash "\" with ordinary characters. The escape character contains at least two characters, the first character is the escape symbol "\", and the second character is the character to be represented. There are two commonly used escape characters: escape characters that represent formats and escape characters that represent control codes.

1. Presentation format

To form characters and strings, double quotation marks (") and single quotation marks (') are used. in order to form an escaped character, a backslash (\) is used. if you need to include these three characters in a string, you need to use escape characters as shown in Table 1.1.

Table 1.1 shows the list of escaped characters in the format

\'

\ "

\\

[example 1-4] has the following text content, which needs to be represented by a string in the program.

I love C#.

I am studying Che.

In this text content, the first line is expressed as a string of "I love clocking.", which is 10 in length (spaces vs.. All count as one character). The second line has a single quotation mark, so you need to use an escape character to express it as "I\ m studying clocking." the string is 16 in length.

2. Represents the control code

In the text data, there will be some special symbols, such as newline characters, backspace and so on. These symbols are not visible, but are used to control the formatting of the text. Since such characters cannot be written, they must be represented by escape characters as shown in Table 1.2.

Table 1.2 represents the escape character sequence of the control code

\ a

\ b

\ f

\ n

\ r

\ t

\ v

In example 1-4, because the two text contents are not on the same line, there must be a "\ n" between them to indicate a line break. Therefore, the correct representation of the two texts is "I love centering.\ n I\'m studying caching." the length of this string is 27.

A more comprehensive expression-- ASCII table

Escaping characters can solve most of the character expressions. However, there are still many characters that cannot be written directly, and there are no corresponding escape characters. At this point, you can only use ASCII to express it. ASCII is a computer coding system based on the Latin alphabet (26 English letters). It is now the most commonly used single-byte coding system. The format of the characters represented by ASCII is shown in Table 1.3.

Table 1.3 characters represented by ASCII

\ 0

\ ddd

Any character represented by a 3-digit octal number

\ xhh

Any character represented by a 2-bit hexadecimal number

In Table 1.3, any character represented by an octal number can be represented by octal, that is, the total length of such escaped characters is 2x4. When using hexadecimal characters, you can represent any character represented by a 1-bit 2-bit hexadecimal number, that is, the total length of this type of escape character is 3-4, because the "\ x" begins with the hexadecimal number.

[example 1-5] escape characters are used in the following code:

'\ x2f'

'\ 013'

Where\ x indicates that the following characters are hexadecimal numbers, and\ 0 means that the following characters are octal numbers.

For a more comprehensive representation, the character 0x127 of the ASCII table is given here. The details are shown in Table 1.4.

Table 1.4 ASCII table

0

00

32

20

[space]

64

40

@

96

60

`

1

01

33

21

!

65

41

A

97

61

A

2

02

34

22

"

66

42

B

ninety-eight

sixty-two

B

three

03

thirty-five

twenty-three

#

sixty-seven

forty-three

C

ninety-nine

sixty-three

C

four

04

thirty-six

twenty-four

$

sixty-eight

forty-four

D

one hundred

sixty-four

D

five

05

thirty-seven

twenty-five

%

sixty-nine

forty-five

E

one hundred and one

sixty-five

E

six

06

thirty-eight

twenty-six

&

seventy

forty-six

F

one hundred and two

sixty-six

F

seven

07

thirty-nine

twenty-seven

'

seventy-one

forty-seven

G

one hundred and three

sixty-seven

G

eight

08

\ b

forty

twenty-eight

(

seventy-two

forty-eight

H

one hundred and four

sixty-eight

H

nine

09

\ t

forty-one

twenty-nine

)

seventy-three

forty-nine

I

one hundred and five

sixty-nine

I

ten

0A

\ r

forty-two

2A

*

seventy-four

4A

J

one hundred and six

6A

J

eleven

0B

forty-three

2B

+

seventy-five

4B

K

one hundred and seven

6B

K

twelve

0C

forty-four

2C

seventy-six

4C

L

one hundred and eight

6C

L

thirteen

0D

\ n

forty-five

2D

-

seventy-seven

4D

M

one hundred and nine

6D

M

fourteen

0E

forty-six

2E

.

seventy-eight

4E

N

one hundred and ten

6E

N

fifteen

0F

?

forty-seven

2F

/

seventy-nine

4F

O

one hundred and eleven

6F

O

sixteen

ten

?

forty-eight

thirty

0

eighty

fifty

P

one hundred and twelve

seventy

P

seventeen

eleven

?

forty-nine

thirty-one

one

eighty-one

fifty-one

Q

one hundred and thirteen

seventy-one

Q

eighteen

twelve

?

fifty

thirty-two

two

eighty-two

fifty-two

R

one hundred and fourteen

seventy-two

R

nineteen

thirteen

fifty-one

thirty-three

three

eighty-three

fifty-three

S

one hundred and fifteen

seventy-three

S

twenty

fourteen

fifty-two

thirty-four

four

eighty-four

fifty-four

T

one hundred and sixteen

seventy-four

T

twenty-one

fifteen

fifty-three

thirty-five

five

eighty-five

fifty-five

U

one hundred and seventeen

seventy-five

U

twenty-two

sixteen

?

fifty-four

thirty-six

six

eighty-six

fifty-six

V

one hundred and eighteen

seventy-six

V

twenty-three

seventeen

?

fifty-five

thirty-seven

seven

eighty-seven

fifty-seven

W

one hundred and nineteen

seventy-seven

W

twenty-four

eighteen

?

fifty-six

thirty-eight

eight

eighty-eight

fifty-eight

X

one hundred and twenty

seventy-eight

X

twenty-five

nineteen

?

fifty-seven

thirty-nine

nine

eighty-nine

fifty-nine

Y

one hundred and twenty one

seventy-nine

Y

twenty-six

1A

?

fifty-eight

3A

:

ninety

5A

Z

one hundred and twenty two

7A

Z

twenty-seven

1B

fifty-nine

3B

ninety-one

5B

[

one hundred and twenty three

7B

{

twenty-eight

1C

?

sixty

3C

ninety-four

5E

^

one hundred and twenty six

7E

~

thirty-one

1F

sixty-three

3F

?

ninety-five

5F

_

one hundred and twenty seven

7F

?

Many of the characters in Table 1.4 cannot be displayed, so they are displayed in white space. In coding, characters can be used by combining a backslash "\" with the hexadecimal number of ASCII characters. For example, "\ n" and "\ 0D" play the same role. But most characters can be output directly, and you don't need to use this way.

Avoid escaping characters-- @

In programming, sometimes the combination of characters happens to be the same as the escape character, and it is necessary to invalidate the escape character in that combination. There is a special character "@" in C # that declares the literal string and ignores the escape character.

[example 1-6] if you want to represent an address "c:\ temp". When we output "c:\ temp" in the code, the result is shown as follows:

C: emp

Why is such a result displayed? Because in "c:\ temp", the "\ t" in the string is recognized by the computer as an escaped character, it acts as a horizontal tab. In this case, you can use the escape character "\" to output the backslash so that the string represents an address as follows:

"c:\\ temp"

This representation can temporarily solve the simple address in front of you. However, this kind of writing can be very cumbersome if the address you want to represent is long and complex and contains multiple backslashes. For example, when the address to be represented is "c:\ temp\ 1\ t\ 3\ a\ 5", it is expressed in this way as:

"c:\\ temp\\ 1\ t\ 3\ a\\ 5"

The above expression is very tedious and prone to writing mistakes. Therefore, you can use the special symbol @ to mask the role of escape characters. When we want to represent the address "c:\ temp\ 1\ t\ 3\ a\ 5", we just need to precede the address with the special character @, as follows:

@ "c:\ temp\ 1\ t\ 3\ a\ 5"

String length

Above, we learned about characters, strings, escape characters, and so on, and then we learned their respective character lengths. The types, examples, length, and output of various characters and strings are shown in Table 1.5:

Table 1.5 string length

/ string example

'a'

one

A

"abc"

three

Abc

"ab\"

four

Ab'c

"ab\ nc"

four

Ab

C

String represented by ASCII

"ab\ x22"

three

Ab "

@ string

@ "ab\ x22"

6

Ab\ x22

The above is all the contents of the article "sample Analysis of the literal amount of C # Grammar text". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 218

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report