Open In App

C# | TrimStart() and TrimEnd() Method

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Trim() Method in C#

In C#, TrimStart() & TrimEnd() are the string methods. TrimStart() method is used to remove the occurrences of a set of characters specified in an array from the starting of the current String object. TrimEnd() method is used to remove the occurrences of a set of characters specified in an array from the ending of the current String object. 

Syntax for TrimStart() Method :

public string TrimStart(params char[] trimChars)

Syntax for TrimEnd() Method :

public string TrimEnd(params char[] trimChars)

Explanation: Both the method will take an array of Unicode characters or null as a parameter. Null is because of params keyword. And the return type value for both the methods is System.String.

Below are the programs to demonstrate the above methods :  

  • Example 1: Program to demonstrate the public string TrimStart(params char[] trimChars) method. This method removes all leading white-space characters from the current string object. Each leading trim operation stops when a non-white-space character is encountered. For example, if the current string is “*****0000abc000****” and trimChars contains the “*” and “0”, then TrimStart method returns “abc000****”.

C#




// C# program to illustrate 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'));
    }
}


Output: 

Before:
*****0000abc000****
  abc
  -GFG-
  GeeksforGeeks

After:
abc000****
abc
  -GFG-
forGeeks

 

  • Example 2: Program to demonstrate the public string TrimEnd(params char[] trimChars) method. This method removes all trailing characters which are present in the parameter list. Each trailing trim operation stops when a non-white-space character is encountered. For example, if the current string is “*****0000abc000****” and trimChars contains the “*” and “0”, then TrimEnd method returns “*****0000abc”.

C#




// 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'));
       }
}


Output: 

Before:
*****0000abc000****
abc  
  -GFG-  
  GeeksforGeeks

After:
*****0000abc
abc
  -GFG-  
  Geeksfor

 

Note: If no parameter will pass in both the method’s arguments list then Null , TAB, Carriage Return and White Space will automatically remove from starting(for TrimStart() method) and ending(for TrimEnd() method) from the current string object. And If any parameter will pass to both the methods then the only specified character(which passed as arguments) will removed from the current string object. Null, TAB, Carriage Return, and White Space will not remove automatically if they are not specified in the arguments list of both the methods.

References:  

 



Last Updated : 10 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads