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

How to use the C # string

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of how to use the C # string, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use the C # string. Let's take a look.

1. Create a string

Strings are reference data types, and you need to use the string keyword when declaring variables.

For example:

String str = "This is a string!"

It is worth noting that in C #, a variable of type string must be a string of characters enclosed in double quotes. The character type (char) is a character enclosed in single quotation marks.

2. Concatenate strings

There are many ways to concatenate strings in C#. Here are some common stitching methods:

(1) use splicing symbols

Strings can be concatenated directly using the + arithmetic operator.

String S1 = "Hello"; string S2 = "World"; string S3 = S1 + S2 Shield Console.WriteLine (S3); / / HelloWorld

Strings can be concatenated not only between strings, but also with all data types. This is because all data types inherit from the base class Object, and there are toString methods in Object. Therefore, all types can theoretically be converted to string types as long as the subclass does not override this method.

String s = "asdf"; int I = 3x float f = 3.2f Bool b = true;Console.WriteLine (s + I); / / asdf3Console.WriteLine (s + f); / / asdf3.2Console.WriteLine (s + b); / / asdftrue (2) string.Concat

Call the string.Concat () method, passing in parameters to complete the concatenation of the string. The number of parameters is at least two.

String S1 = "I"; string S2 = "\'m"; int I = 18 × string s = string.Concat (S1, S2, I); Console.WriteLine (s); / / I'm 18 (3) placeholder splicing string name = "Xiaoming"; int age = 22 × string s = string.Format ("{0} this year {1} years old", name, age); Console.WriteLine (s); / / Xiaoming 22 years old (4) $splicing

The $mode that began to appear in C # 6.0 can also be used to concatenate strings. It's actually a simplified version of string.Format.

String name = "Xiaogang"; int num = 3X string s = $"{name} built a floor {num} m high." ; Console.WriteLine (s) / / Xiaogang built a floor 3 meters high. 3. Compare strings

Strings can also be compared, and the comparison methods are as follows:

(1) use the comparison operator string S1 = "Woooo"; string S2 = "Wooooa"; bool b = S1 = = S2 Tabacer / false (2) string.Compare

String.Compare (S1, S2) compares the characters of the two strings passed in, and returns 0 if the Unicode coding value of each character is the same; if the encoding value of a character is different, the former immediately returns 1; otherwise, it returns-1.

String S1 = "abb"; string S2 = "abc"; string S3 = "caa"; string S4 = "abc"; int N1 = string.Compare (S1, S2); / /-1int N2 = string.Compare (S3, S1); / / 1int N2 = string.Compare (S2, S4); / / 0

In this method, you can also pass in a third parameter, the Boolean type. When the Boolean parameter is true, it is not case-sensitive.

String S1 = "abc"; string S2 = "ABC"; int N1 = string.Compare (S1, S2); / /-1int N2 = string.Compare (S1, S2, true); / / 0 (3) string.Equals

Sting.Equals (S1, S2) is very similar to the above string.Compare, except that the former returns true or false.

String S1 = "abc"; string S2 = "bbc"; string S3 = "aac"; string S4 = "abc"; bool b1 = string.Equals (S1, S2); / / falsebool b2 = string.Equals (S1, S3); / / falsebool b3 = string.Equals (S1, S4); / / true4, placeholder

There is already a demonstration of the use of placeholders.

The placeholder, as the name implies, is to give you a place in advance and mark it. Wait for someone to come and sit down in the marked order.

Int N1 = 1 position int N2 = 2 politics int n3 = 3 position Console.WriteLine ("{1} position, {0} position, {2} position", N1, N2, n3); / / 2nd position, 1st position, 3rd position

Then there must be such a situation, on the way here, someone suddenly wants to join in, but the seats are arranged in advance, as a result, there is not enough seats, what will happen?

Int N1 = 1 position int N2 = 2 position int n3 = 3 position int N4 = 4 position Console.WriteLine ("{1} position, {0} position, {2} position", N1, N2, n3, N4); / / 2nd position, 1st position, 3rd position

It is obvious that people who want to be opportunistic only deserve to stand.

If this person is immoral and not only joins in midway, but also gets in front of the third person, then he succeeds.

Int N1 = 1 position int N2 = 2 position int n3 = 3 position int N4 = 4 position Console.WriteLine ("{1} position, {0} position, {2} position", N1, N2, n4, n3); / / 2nd position, 1st position, 4th position

Those who have been cut in line are sorry to have no choice but to stand!

Sometimes the organizers are smart enough to expect that some people will come uninvited, so more seats will be arranged at this time. But as a result, fewer people come, places are redundant, what will happen?

Int N1 = 1x int N2 = 2th Console.WriteLine ("position {1}, location {0}, location {2}", N1, N2)

The people who came found that there were so many seats left unattended. It seems that the organizer can't do it. The show is definitely not good-looking. Break up!

As can be seen from the above, the position can be less, but not more; the value can be more, but must not be less, otherwise such an exception will be thrown.

5. Escape characters

Transfer symbol\

An escape character is a special character consisting of a transfer symbol plus a specific character that can be programmed to express something:

The escape character acts as\ 'display single quotation marks in string\ "display double quotation marks\ t tab character in the string, equivalent to tab key\ nWrap, equivalent to hitting the return key\\ display the slash symbol string s =" Xiao Ming said:\ "I am handsome!\"\ nXiao Hong said:\ "\ t\\ yue\ t\"; Console.WriteLine (s)

This is the end of the article on "how to use the C # string". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use C# strings". If you want to learn more, you are 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: 0

*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