C# data structure string (string)

Abstract: Explain the use of C# strings in detail with specific examples.

Programming language: C#

Programming environment: Visual Studio 2019

Table of contents

escape character sequence

string creation 

declaration of string

null string and empty string 

Constructs that repeat single-character strings

String Properties and Common Methods

Attributes 

common method

summary

each message


        A string represents a text object, which means that characters are stringed together one by one, and it is regarded as a sequential read-only collection of Char objects , which is widely used. 

escape character sequence

        Before talking about strings, let's talk about characters. I don't know if you will notice in programming that there are often some special characters in characters or strings. For example, if we want to define a character, this character is a single quotation mark, but an error will be prompted if the single quotation mark is entered directly. Is it possible that such a string cannot be defined? That would underestimate the C# language. For some characters that conflict with the grammar or have special meanings, the character "\" is specified as an escape character, and the following character must be treated specially, and its meaning is determined according to the following character. Commonly used escape character sequences are as follows.

Common escape character sequences
escape character sequence character name program example operation result
\' apostrophe Write("C\'#"); C'#
\" Double quotes Write("C\"#"); C"#
\n line break Write("C\n#");

C

#

\t Tab key (space) Write("C\t#") C #
\r Carriage return (return to the beginning of the line) Write("C\r#") #C
\\ backslash Write("C\\#") C\#

string creation 

declaration of string

The declaration of a string is very simple. Note that when assigning a string, double quotes         must be added around the content of the string . The syntax of the declaration is as follows.

string str = "字符串的内容";

null string and empty string 

        Here is a special explanation of the creation and judgment of empty strings. First, the creation of empty strings can be done in the following three ways. The three methods are not exactly the same. When the string is null, if you access its properties, an exception will result. string.Empty and "" strings represent empty strings, that is, strings with a length of zero. Therefore, string.Empty or "" is often used when emptying a string . I didn't understand the relationship between the three before, and I used null to empty the string, which caused a lot of exceptions and suffered a lot.

string strNull = null;//null字符串,若访问其成员,会导致异常
string strEmpty = string.Empty;//空字符串,长度为零
string str = "";//空字符串,长度为零

       

         After understanding the creation of empty strings, it is easy to judge. There are the following ways to judge empty strings. Among them, null only corresponds to the judgment of the null string. The empty string string.Empty and "" can be equivalent to each other. string.IsNullOrEmpty includes the judgment of both the null string and the empty string. Judgment of a string composed of several blank characters.

if (str == null)
{
    //仅在str=null时返回true
}
else if (str == "")
{
    //在str为空字符串时返回true
}
else if (str == string.Empty)
{
    //在str为空字符串时返回true
}
else if (string.IsNullOrEmpty(str))
{
    //在str=null或空字符串时返回true
}
else if (string.IsNullOrWhiteSpace(str))
{
    //在str=null、空字符串或若干空白字符组成的字符串时返回true
}

Constructs that repeat single-character strings

        The string string gives its construction method, the constructor is as follows, the function is to construct a string composed of count ch characters.

String(char ch, int count);//重复单字符组成的字符串构造

String Properties and Common Methods

Attributes 

  • Length: Gets the (read-only) length of the string.
  • this[index]: Get (read-only) the character at the specified index position index.

common method

  • CompareTo(str): Determines whether the current string is equal to str, and returns 0 if they are equal. (Note: To judge string equality, you can also directly combine the two strings to be judged with the logical equal symbol "==")
  • ToUpper(): Convert all characters of the string to uppercase characters.
  • ToLower(): Convert all characters of the string to lowercase characters.
  • Insert(index, str): Returns the string after inserting the string str after the index position index.
  • Remove(index,count): Returns a string that deletes count characters starting from the index position index.
  • Replace(oldchar, newchar): Returns a string that replaces all oldchar characters in the string with newchar characters.
  • Replace(oldstr, newstr): Returns all strings containing the oldstr string in the string replaced by the newstr string.

         Test the Replace method, the code is as follows.

string str1 = "She is a girl";
Console.WriteLine(str1.Replace("girl", "boy"));
Console.WriteLine(str1.Replace('i', 'E'));

        operation result.

 

  • Substring(index,count): Returns a string of count characters intercepted from the index position index.
  • Substring(index): Returns the string intercepted from the index position index to the end.
  • Split(char): Returns an array of strings separated by character char. (Note: The elements of the split string array do not include the separator char itself)

        Test the Split method, the code is as follows.

string str2 = "a,b,c,d";
string[] STR = str2.Split(',');
            
for(int i = 0; i < STR.Length; i++)
{
    Console.WriteLine(STR[i]);
}

        operation result.

  • IndexOf(char): Returns the index number of the first character char matched from small to large.
  • LastIndexOf(char): Returns the index number of the first character char matched from big to small. 
  • StartsWith(str): Whether the beginning of the matching string is str, it returns true, otherwise it returns false.
  • EndsWith(str): Whether the end of the matching string is str, it returns true, otherwise it returns false.

summary

        This article first explains the escape character sequence, that is, how to enter conflicting characters and characters that represent special meanings; secondly, the creation of strings, including declarations, the difference between the creation and judgment of empty strings and null strings, and strings of repeated characters Construction; finally, the function of the string is explained in detail from the properties and methods of the string.

each message

        Mistakes are inevitable, but don't repeat them.

 

Guess you like

Origin blog.csdn.net/lucgh/article/details/130566324