Summary of String Class Common Methods for C#

Posted by pauls74462 on Sat, 13 Jun 2020 02:55:00 +0200

Summary of String Class Common Methods for C#

The String class represents text, a series of Unicode characters.In the namespace System.String is Unicode
An ordered collection of characters used to represent text.String objects are an ordered collection of System. Char objects that represent strings.String
The value of the object is the content of the ordered collection and is immutable.

1.Compare method

The Compare method has several overloading methods that perform a comparison of two strings according to specified rules and ranges. The method returns an integer describing the relative position in the sorting of the two strings.

String str1 = "czhenya";
String str2 = "Czhenya_CSDN";

//Parameters: String 1, String 2, case sensitive
Console.WriteLine(String.Compare(str1, str2));       //Output: -1
Console.WriteLine(String.Compare(str1, str2, true)); //Output: -1

//Parameters: string 1,str1 start index, string 2,str2 start index, compare length, case sensitive
Console.WriteLine(String.Compare(str1, 0, str2, 0, 7));       //Output: -1
Console.WriteLine(String.Compare(str1, 0, str2, 0, 7, true)); //Output: 0

2.Concat method

Concat methods can be overloaded in a variety of ways to connect to a specified string or object.

  String s1 = "C";
  String s2 = "Zhen";
  String s3 = "Ya";
  String s4 = "_CSDN";
  // This writing supports concatenation of up to four strings
  Console.WriteLine(String.Concat(s1, s2));           //Output: CZhen          
  Console.WriteLine(String.Concat(s1, s2, s3, s4));   //Output: CZhenYa_CSDN
  // There are also three object s that are overloaded with parameter types
  Console.WriteLine(String.Concat(s1, 1, '2', 0.3f)); //Output: C120.3

  // Connection String Array
  String[] strArr = { "A", "BC", "DEF", "GH", "IJKL" };
  Console.WriteLine(String.Concat(strArr));     //Output: ABCDEFGHIJKL

3.Contains method

Returns a bool value indicating whether the specified String object appears in this string.

 String str1 = "Czhenya";
 String str2 = "Czhenya_CSDN";

 // Whether str1 contains str2 (case sensitive) --> or whether str2 is a string of str1
 Console.WriteLine(str1.Contains(str2));     //Output: False
 Console.WriteLine(str2.Contains(str1));     //Output: True
 // Parameter is an overload of type char
 Console.WriteLine(str1.Contains('c'));     //Output: False
 Console.WriteLine(str1.Contains('C'));     //Output: True

4.StartsWith and EndsWith methods

Returns a Boolean value determining whether the start/end of this string instance matches the specified string.

 String str1 = "Czhenya";
 String str2 = "Czhenya_CSDN";
 
 //Does str1 start with str2
 Console.WriteLine(str1.StartsWith(str2));     //Output: False
 Console.WriteLine(str1.StartsWith('C'));      //Output: True

 Console.WriteLine(str2.StartsWith(str1));     //Output: True
 Console.WriteLine(str2.StartsWith('c'));      //Output: False

5.Equals method

The Equals method can be overloaded in a variety of ways to determine whether two String instances have the same value according to certain rules.

 String str1 = "Czhenya";
 String str2 = "czhenya";

 //Parameter is of type object
 Console.WriteLine(str1.Equals(str2));           //Output: False
 // Comparing str1 with str2
 Console.WriteLine(String.Equals(str1,str2));    //Output: False
 // Comparing str1 with str2 is case insensitive
 Console.WriteLine(String.Equals(str1,str2, StringComparison.OrdinalIgnoreCase)); 
 //Output: True

6.ToLower method and ToUpper method

Converts a character in a string to lowercase or to uppercase.

  String str1 = "Czhenya";
  String str2 = "czhenya";
  
  Console.WriteLine(str1.ToLower());    //Output: czhenya
  Console.WriteLine(str2.ToUpper());    //Output: CZHENYA

7.Substring method

Substrings have two overload modes that allow you to Substring a string at a specified starting position and length.

 String str1 = "Czhenya";
 //Parameter: Start Index
 Console.WriteLine(str1.Substring(2));     //Output: henya
 //Parameters: start index, intercept length
 Console.WriteLine(str1.Substring(0, 2));  //Output: Cz

8.Split method

Split, which implements the function of splitting strings into multiple substrings according to specified characters.

    //Use underscore'_"Delimited string
    String str2 = "Czhenya_C_S_D_";
    string[] res2 = str2.Split('_');
    for (int i = 0; i < res2.Length; i++)
    {
          Console.Write(res2[i] + "..."); // Output: Czhenya...C...S...D...
    }
    Console.WriteLine();
    // Default parameters:StringSplitOptions.None: The return value includes an array element containing an empty string.
    // StringSplitOptions.RemoveEmptyEntries: The return value does not include an array element containing an empty string.
    string[] res3 = str2.Split('_', StringSplitOptions.RemoveEmptyEntries);
    for (int i = 0; i < res3.Length; i++)
    {
         Console.Write(res3[i] + "..."); // Output: Czhenya...C...S...D...
    }
    Console.WriteLine();

    //Parameters: Separate characters, separate groups, default parameters:StringSplitOptions.None
    string[] res4 = str2.Split('_',3);
    for (int i = 0; i < res4.Length; i++)
    {
          Console.Write(res4[i] + "..."); // Output: Czhenya...C...S_D_...
    }
    Console.WriteLine();

9.Replace method

The Replace method replaces the character or substring specified in this instance.

   String str1 = "Czhenya_CSDN";
   // Replace the character'C'in the string with the character'A'
   Console.WriteLine(str1.Replace('C','A'));           //Output: Azhenya_ASDN
   // String "ABCD", replacing string "CSDN"
   Console.WriteLine(str1.Replace("CSDN", "ABCD"));    //Output: Czhenya_ABCD

10.Remove method

The Remove method deletes the character specified in this instance.

 String str1 = "Czhenya_CSDN";
 //Parameter: Index to start removing
 Console.WriteLine(str1.Remove(7));           //Output: Czhenya
 //Parameter: Start removing index, remove length
 Console.WriteLine(str1.Remove(0,2));         //Output: henya_CSDN