C × basic knowledge series - more usage of 9 strings

0. Preface

In the previous article, I briefly introduced the relevant content of string, but didn't involve more relevant content. This article will try to explain a lot of string operations that will be encountered in the actual development work.

1. Create a string

This section describes how to create a string C ා basic knowledge series - 2 strings It introduces how to create a string object through initialization and simple string assignment. This part is supplemented and expanded here.

1.1 Object.ToString method

There is a ToString method inherited from the Object for any Object of C ා, which returns a string without extra parameters. Without rewriting, the method returns the full class name (the so-called full class name is the representation of namespace. Class name).

Why do you need to introduce this method? Because this method runs through the string function logic of C ා, this method is also used to obtain the literal description of an object.

1.2 addition of strings

What is the 'addition' operation of strings?

For example:

string str1 = 1 + "123";// 1123
string str2 = 2.3 + "123";//2.3123            

As described in the above example, similar to the form of "object" + "string", I call it an "addition" operation.

Why is it called "addition"?

In fact, the official did not make a clear definition of this way of declaration, and stipulate its naming method. I call it "addition" because there are many similarities between this method and numerical addition, such as the priority problem.

Example:

string str1 = 1 + "1" + 123; // 11123
string str2 = 1 + 1 + "123"; // 2123
string str3 = "1" + 1 + 123; // 11123

For the above three examples, it will be very troublesome to understand by string connection, for example, why 1 + 1 in str2 will operate first, why str3 is not 1124, etc. If you understand it by adding, it's easy to understand. str2 calculates 1 + 1 = 2, then 2 + "123"; str3 calculates "1" + 1 = "11" and then "11" + 123.

In short, in C ා, the string's' addition 'operation will be performed in the normal number's addition order. If the calculation of object + string is encountered, the ToString of the object is called to get the string description of the object, and then the description is connected with the string.

1.3 format of string

The string.Format method C ා provides many overloaded versions. Here is a brief introduction to one of the commonly used writing methods:

string str = string.Format("{0}-{1}",1,2);//1-2

The parameter list of this method can refer to:

public static string Format(string format,params object[] objs);
//params keyword indicates the list of variable parameters, followed by the declaration of an array, indicating that this parameter can be any one, and can be used by array within the method

Of course, it's not. Where the format parameter is a format string, where {,} wraps a number as a placeholder, indicating that the next few parameters in the objs parameter are replaced in this position.

C ා in the subsequent version updates, a writing method has been added for the format of strings:

int a = 10;
double b = 10.3;
string str = $"{a}{b}";

This method starts with the dollar sign $before the double quotation mark of the string, and then wraps the variable name with {,} to indicate that the variable name is replaced with the corresponding position. This method is simple and clear, and there is no need to worry about the wrong number of placeholders. The number of objs is more than 5. Once the content of format is too long, it is easy to mark the number of placeholders incorrectly.

There is a question. What if the string itself contains "{" or "}"? The solution is simple:

int a = 10;
double b = 10.3;
string str = $"{{c}}{a}{b}";// {c}1010.3

Just add the same brace outside the brace.

Here is a brief introduction to another important content of string formatting, data formatting. Usage is to add: < format expression >, a colon after a number or variable, and then write a format expression after the placeholder wrapped in braces.

String formatting of numbers

string.Format("{0:#0.00}-{1}",1,2); 
// #0.00 means to generate a numeric string with two decimal places, and 0 is used to supplement the less than two decimal places

String format for date

DateTime date = DateTime.Now;
string str = $"{date:yyyy-MM-dd}";// 2020-04-15

Here is a brief introduction to the content of string formatting. More content can be found in the official documents of Microsoft or expected to be introduced in the following articles.

2. String search

2.1 whether it is the beginning or end of a string

public bool StartsWith(string value);
public bool EndsWith(string value);

Returns whether the beginning or end of a string matches value.

Example:

var str = "aisbjaowiue93kas";
str.StartsWith("1");// false
str.StartsWith("ai");// true

2.2 include a string

public bool Contains(string value);

Checks whether the source string contains a substring that is equal to value.

2.3 find a place

public int IndexOf(char value);//Find the first occurrence of the character value in the string
public int IndexOf(char value, int startIndex);//Find the first occurrence of the character value in the string after startIndex
public int IndexOf(string value);//Find the first occurrence of the substring value in the string
public int IndexOf(string value, int startIndex);//Find the position where the substring value first appears after startIndex

The above method also has a similar method LastIndexOf. As the name implies, it returns the location of the last occurrence. The parameters are the same as the above method.

It is worth noting that all subscripts or indexes are evaluated from 0, and - 1 is returned if they are not found.

3. Modify and decompose string

This section describes how to modify and cut strings.

3.1 substitution

public string Replace(string oldValue,string newValue);
public string Replace(char oldValue,char newValue);

Replace the oldValue in the string with newValue. If the string does not contain oldValue, the string will not change.

3.2 pinching strings

Head to tail operation:

public string Trim();// Remove spaces at the beginning and end of a string
public string Trim(char trimChar);//Remove the characters trimChar from the beginning and end of the string
public string Trim(params char[] trimChars);// Remove all characters contained in trimChars from the beginning and end of a string

Operation head only:

public string TrimStart();//Remove the space at the beginning of the string
public string TrimStart(char trimChar);//Remove the character trimChar at the beginning of the string
public string TrimStart(params char[] trimChars);// Remove all characters contained in trimChars from the header of the string

Last operation tail:

public string TrimEnd();//Remove the space at the beginning of the string
public string TrimEnd(char trimChar);//Remove the character trimChar at the beginning of the string
public string TrimEnd(params char[] trimChars);// Remove all characters contained in trimChars from the header of the string

There is a problem here. Strings can't be processed in the form of characters. In particular, special attention should be paid to the processing of params char[] trimChars.

3.3 case switching

public string ToLower();// Convert to lowercase
public string ToUpper();// Convert to uppercase

We usually refer to the case conversion of English characters. The case conversion of other languages is related to the corresponding language settings. Chinese and numbers will not change, especially Chinese will not be traditional or simplified.

3.4 decomposing strings

public string[] Split(char separator, StringSplitOptions options = StringSplitOptions.None);
public string[] Split(string separator, StringSplitOptions options = StringSplitOptions.None);

Both of the above methods decompose the string into a string array, where the separator is the separator.

The above are some methods of String itself. The next chapter will introduce some common methods of String class. These two parts constitute the String part of C ා.

Please pay attention to more My blog

Tags: C# less

Posted on Wed, 15 Apr 2020 12:14:32 -0700 by polson