Summary of methods for manipulating strings in C#

Easy to check

string s ="";
            //(1) Character access (subscript access s[i])
            s ="ABCD";
            Console.WriteLine(s[0]); // 输出"A";
            Console.WriteLine(s.Length); // 输出4
            Console.WriteLine();

            //(2) Break up into a character array (ToCharArray)
            s ="ABCD";
            char[] arr = s.ToCharArray(); // break the string into a character array {'A','B','C','D'}
            Console.WriteLine(arr[0]); // output the first element of the array, output "A"
            Console.WriteLine();

            //(3) Intercept substring (Substring)
            s ="ABCD";
Console.WriteLine(s.Substring(1)); // Start from the 2nd bit (index starts from 0) and intercept until the end of the string, output "BCD"
            Console.WriteLine(s.Substring(1, 2)); // Truncate 2 bits from the 2nd bit, output "BC"
            Console.WriteLine();

            //(4) Match index (IndexOf())
            s ="ABCABCD";
            Console.WriteLine(s.IndexOf('A')); // Search the position index of the first matching character A from the beginning of the string, output "0"
            Console.WriteLine(s.IndexOf("BCD")); // Search for the first matching string BCD from the beginning of the string, output "4"
            Console.WriteLine(s.LastIndexOf('C')); // Search for the first matching character C from the end of the string, output "5"
            Console.WriteLine(s.LastIndexOf("AB")); // Search for the first matching string BCD from the end of the string, output "3"
            Console.WriteLine(s.IndexOf('E')); // Search for the first matching string E from the head of the string, no matching output "-1";
            Console.WriteLine(s.Contains("ABCD")); // Determine if there is another string "ABCD" in the string, output true
            Console.WriteLine();

            //(5) Case conversion (ToUpper and ToLower)
            s ="aBcD";
            Console.WriteLine(s.ToLower()); // Convert to lowercase, output "abcd"
            Console.WriteLine(s.ToUpper()); // Convert to uppercase, output "ABCD"
            Console.WriteLine();

            //(6) Padding alignment (PadLeft and PadRight)
            s ="ABCD";
            Console.WriteLine(s.PadLeft(6, '_')); // Pad the left part of the string with '_' to expand it to a total length of 6 bits, output "__ABCD"
            Console.WriteLine(s.PadRight(6, '_')); // Pad the right part of the string with '_' to expand it to a total length of 6 bits, output "ABCD__"
            Console.WriteLine();

            //(7) Truncate and remove the tail (Trim)
            s ="__AB__CD__";
            Console.WriteLine(s.Trim('_')); // remove the '_' characters at the beginning and end of the string, output "AB__CD"
            Console.WriteLine(s.TrimStart('_')); // remove the '_' character at the head of the string, output "AB__CD__"
            Console.WriteLine(s.TrimEnd('_')); // remove the trailing '_' character in the string, output "__AB__CD"
            Console.WriteLine();

            //(8) Insert and delete (Insert and Remove)
            s ="ADEF";
            Console.WriteLine(s.Insert(1, "BC")); // Insert the string "BC" at position 2 of the string, output "ABCDEF"
            Console.WriteLine(s);
            Console.WriteLine(s.Remove(1)); // Delete characters from the 2nd to the end of the string, output "A"
            Console.WriteLine(s);
            Console.WriteLine(s.Remove(0, 2)); // delete 2 characters from the first position of the string, output "EF"
            Console.WriteLine();

            //(9) Replace character (string) (Replace)
            s ="A_B_C_D";
            Console.WriteLine(s.Replace('_', '-')); // Replace the '_' character in the string with '-', output "ABCD"
            Console.WriteLine(s.Replace("_", "")); // Replace "_" in the string with an empty string, output "ABC D"
            Console.WriteLine();

            //(10) Split into a string array (Split) - reciprocal operation: combine a string static method Join(seperator, arr[])
            s ="AA,BB,CC,DD";
            string[] arr1 = s.Split(','); // Split the string with ',' characters and return an array of strings
            Console.WriteLine(arr1[0]); // output "AA"
            Console.WriteLine(arr1[1]); // 输出"BB"
            Console.WriteLine(arr1[2]); // 输出"CC"
            Console.WriteLine(arr1[3]); // output "DD"
            Console.WriteLine();

            s ="AA--BB--CC--DD";
            string[] arr2 = s.Replace("--", "-").Split('-'); // Technique for splitting strings: first replace the string "--" with a single character "-" ", then split the string with the '-' character and return an array of strings
            Console.WriteLine(arr2[0]); // 输出"AA"
            Console.WriteLine(arr2[1]); // 输出"BB"
            Console.WriteLine(arr2[2]); // 输出"CC"
            Console.WriteLine(arr2[3]); // 输出"DD"
            Console.WriteLine();

            //(11) Format (static method Format)
            Console.WriteLine(string.Format("{0} + {1} = {2}", 1, 2, 1+2));
            Console.WriteLine(string.Format("{0} / {1} = {2:0.000}", 1, 3, 1.00/3.00));
            Console.WriteLine(string.Format("{0:yyyy年MM月dd日}", DateTime.Now));

            //(12) Concatenate into a string (static method Concat, static method Join and instance method StringBuilder.Append)
            s ="A,B,C,D";
            string[] arr3 = s.Split(','); // arr = {"A","B","C","D"}

            Console.WriteLine(string.Concat(arr3)); // Concatenate an array of strings into a string, output "ABCD"

            Console.WriteLine(string.Join(",", arr3)); // Use "," as the dividing symbol to join a string array into a string, output "A,B,C,D"

            StringBuilder sb =new StringBuilder(); // Declare a String Builder instance
            sb.Append("A"); // use the string constructor to concatenate strings for better performance
            sb.Append('B');
            Console.WriteLine(sb.ToString());// 输出"AB"

      //(13) Two ways to output double quotes for strings
      Console.WriteLine("this is a \"test\"!");
      Console.WriteLine(@"this is a ""test""!");



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325725004&siteId=291194637