C# | Trim() Method

C# Trim() is a string method. This method is used to removes all leading and trailing white-space characters from the current String object. This method can be overloaded by passing arguments to it.

Syntax:

public string Trim()  
or
public string Trim (params char[] trimChars)

Explanation : First method will not take any parameter and the second method will take an array of Unicode characters or null as a parameter. Null is because of params keyword. The type of Trim() method is System.String.

Note: If no parameter is pass in public string Trim() then Null , TAB, Carriage Return and White Space will automatically remove if they are present in current string object. And If any parameter will pass into the Trim() method then only specified character(which passed as arguments in Trim() method) will be 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.

Below are the programs to demonstrate the above method :

  • Example 1: Program to demonstrate the public string Trim() method. The Trim method removes all leading and trailing white-space characters from the current string object. Each leading and trailing trim operation stops when a non-white-space character is encountered. For example, If current string is ” abc xyz ” and then Trim method returns “abc xyz”.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the 
    // method without any parameters
    using System;
      
    class GFG {
          
        // Main Method
        public static void Main()
        {
            string s1 = " GFG";
            string s2 = " GFG ";
            string s3 = "GFG ";
      
            // Before Trim method call
            Console.WriteLine("Before:");
            Console.WriteLine(s1);
            Console.WriteLine(s2);
            Console.WriteLine(s3);
      
            Console.WriteLine("");
      
            // After Trim method call
            Console.WriteLine("After:");
            Console.WriteLine(s1.Trim());
            Console.WriteLine(s2.Trim());
            Console.WriteLine(s3.Trim());
        }
    }

    chevron_right

    
    

    Output:

    Before:
     GFG
     GFG 
    GFG 
    
    After:
    GFG
    GFG
    GFG
    
  • Example 2: Program to demonstrate the public string Trim (params char[] trimChars) method. The Trim method removes from the current string all leading and trailing characters which are present in the parameter list. Each leading and trailing trim operation stops when a character which is not in trimChars encountered. For example, current string is “123abc456xyz789” and trimChars contains the digits from “1 to 9”, then Trim method returns “abc456xyz”.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the 
    // method with parameters
    using System;
      
    class GFG {
          
        // Main Method
        public static void Main()
        {
              
            // declare char[] array and
            // initialize character 0 to 9
            char[] charsToTrim1 = {'1', '2', '3', '4', '5',
                                   '6', '7', '8', '9'};
                                     
            string s1 = "123abc456xyz789";
            Console.WriteLine("Before:" + s1);
            Console.WriteLine("After:" + s1.Trim(charsToTrim1));
      
            Console.WriteLine("");
      
            char[] charsToTrim2 = { '*', '1', 'c' };
            string s2 = "*123xyz********c******c";
            Console.WriteLine("Before:" + s2);
            Console.WriteLine("After:" + s2.Trim(charsToTrim2));
      
            Console.WriteLine("");
      
            char[] charsToTrim3 = { 'G', 'e', 'k', 's' };
            string s3 = "GeeksForGeeks";
            Console.WriteLine("Before:" + s3);
            Console.WriteLine("After:" + s3.Trim(charsToTrim3));
      
            Console.WriteLine("");
      
            string s4 = "     Geeks0000";
            Console.WriteLine("Before:" + s4);
            Console.WriteLine("After:" + s4.Trim('0'));
        }
    }

    chevron_right

    
    

    Output:

    Before:123abc456xyz789
    After:abc456xyz
    
    Before:*123xyz********c******c
    After:23xyz
    
    Before:GeeksForGeeks
    After:For
    
    Before:     Geeks0000
    After:     Geeks
    

Important Points About Trim() Method:

  • If the Trim method removes any characters from the current instance, then this method does not modify the value of the current instance. Instead, it returns a new string in which all leading and trailing whitespace characters of the current instance will be removed out.
  • If the current string equals Empty or all the characters in the current instance consist of white-space characters, the method returns Empty.

Reference: https://msdn.microsoft.com/en-us/library/system.string.trim



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.