📜  C#| TrimStart()和TrimEnd()方法

📅  最后修改于: 2021-05-30 00:48:03             🧑  作者: Mango

先决条件:C#中的Trim()方法

C#中TrimStart()TrimEnd()是字符串方法。 TrimStart()方法用于从当前String对象的开头删除数组中指定的一组字符的出现。 TrimEnd()方法用于从当前String对象的末尾删除数组中指定的一组字符的出现。

TrimStart()方法的语法:

public string TrimStart(params char[] trimChars)

TrimEnd()方法的语法:

public string TrimEnd(params char[] trimChars)

说明:这两种方法都将使用Unicode字符数组或null作为参数。空是因为params关键字。两种方法的返回类型值为System.String

下面是演示上述方法的程序:

  • 示例1:演示公共字符串TrimStart(params char [] trimChars)方法的程序。此方法从当前字符串对象中删除所有前导空格字符。遇到非空格字符时,每个前导修剪操作都会停止。例如,如果当前字符串为“ ***** 0000abc000 ****”,并且trimChars包含“ *”和“ 0”,则TrimStart方法将返回“ abc000 ****”。
    // C# program to illustate the
    // TrimStart() method
    using System;
      
    class GFG {
      
        // Main Method
        public static void Main()
        {
      
            // string to be trimmed
            string s1 = "*****0000abc000****";
      
            char[] charsToTrim1 = { '*', '0' };
      
            // string to be trimmed
            string s2 = "  abc";
            string s3 = "  -GFG-";
            string s4 = "  GeeksforGeeks";
      
            // Before TrimStart method call
            Console.WriteLine("Before:");
            Console.WriteLine(s1);
            Console.WriteLine(s2);
            Console.WriteLine(s3);
            Console.WriteLine(s4);
      
            Console.WriteLine("");
      
            // After TrimStart method call
            Console.WriteLine("After:");
      
            // argument as char array
            Console.WriteLine(s1.TrimStart(charsToTrim1));
      
            // if there is no argument then it 
            // takes default as null, ' ', 
            // '\t', '\r'
            Console.WriteLine(s2.TrimStart());
      
            // White space is not remove
            Console.WriteLine(s3.TrimStart('-'));
      
            // not take char array but Argument only character
            Console.WriteLine(s4.TrimStart(' ', 'G', 'e', 'k', 's'));
        }
    }
    
    输出:
    Before:
    *****0000abc000****
      abc
      -GFG-
      GeeksforGeeks
    
    After:
    abc000****
    abc
      -GFG-
    forGeeks
    
  • 示例2:演示公共字符串TrimEnd(params char [] trimChars)方法的程序。此方法删除参数列表中存在的所有尾随字符。当遇到非空白字符时,每个尾随的修剪操作都会停止。例如,如果当前字符串为“ ***** 0000abc000 ****”,并且trimChars包含“ *”和“ 0”,则TrimEnd方法将返回“ ***** 0000abc”。
    // C# program to illustrate the
    // TrimEnd() method
    using System;
       
    class GFG {
           
         // Main Method
         public static void Main()
        {
              
            // String to be trimmed
            string s1 = "*****0000abc000****";
              
          
            char[] charsToTrim1 = { '*', '0'};
               
            // string to be trimmed
            string s2 = "abc  ";
            string s3 = "  -GFG-  ";
            string s4 = "  GeeksforGeeks";
               
            // Before TrimEnd method call
            Console.WriteLine("Before:");
            Console.WriteLine(s1);
            Console.WriteLine(s2);
            Console.WriteLine(s3);
            Console.WriteLine(s4);
               
            Console.WriteLine("");
               
            // After TrimEnd method call
            Console.WriteLine("After:");
               
            // argument as char array
            Console.WriteLine(s1.TrimEnd(charsToTrim1));
               
            // if there is no argument then it 
            // takes default as null, ' ', 
            // '\t', '\r'
            Console.WriteLine(s2.TrimEnd());
               
            // White space is not remove
            Console.WriteLine(s3.TrimEnd('-'));
               
            // not take char array but 
            // Argument only character
            Console.WriteLine(s4.TrimEnd(' ','G','e','k','s'));
           }
    }
    
    输出:
    Before:
    *****0000abc000****
    abc  
      -GFG-  
      GeeksforGeeks
    
    After:
    *****0000abc
    abc
      -GFG-  
      Geeksfor
    

注意:如果方法的参数列表中均未传递任何参数,则Null,TAB,回车符和空格将从当前字符串对象的start(对于TrimStart()方法)和end(对于TrimEnd()方法)中自动删除。并且,如果任何参数都将传递给这两个方法,则唯一指定的字符(作为参数传递的字符)将从当前字符串对象中移除。如果在两个方法的参数列表中均未指定Null,TAB,回车符和空格,则不会自动删除它们。

参考:

  • https://msdn.microsoft.com/zh-CN/library/system。字符串.trimstart
  • https://msdn.microsoft.com/zh-CN/library/system。字符串.trimend