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”.
// C# program to illustrate the// method without any parametersusingSystem;classGFG {// Main MethodpublicstaticvoidMain(){strings1 =" GFG";strings2 =" GFG ";strings3 ="GFG ";// Before Trim method callConsole.WriteLine("Before:");Console.WriteLine(s1);Console.WriteLine(s2);Console.WriteLine(s3);Console.WriteLine("");// After Trim method callConsole.WriteLine("After:");Console.WriteLine(s1.Trim());Console.WriteLine(s2.Trim());Console.WriteLine(s3.Trim());}}chevron_rightfilter_noneOutput: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”.
// C# program to illustrate the// method with parametersusingSystem;classGFG {// Main MethodpublicstaticvoidMain(){// declare char[] array and// initialize character 0 to 9char[] charsToTrim1 = {'1','2','3','4','5','6','7','8','9'};strings1 ="123abc456xyz789";Console.WriteLine("Before:"+ s1);Console.WriteLine("After:"+ s1.Trim(charsToTrim1));Console.WriteLine("");char[] charsToTrim2 = {'*','1','c'};strings2 ="*123xyz********c******c";Console.WriteLine("Before:"+ s2);Console.WriteLine("After:"+ s2.Trim(charsToTrim2));Console.WriteLine("");char[] charsToTrim3 = {'G','e','k','s'};strings3 ="GeeksForGeeks";Console.WriteLine("Before:"+ s3);Console.WriteLine("After:"+ s3.Trim(charsToTrim3));Console.WriteLine("");strings4 =" Geeks0000";Console.WriteLine("Before:"+ s4);Console.WriteLine("After:"+ s4.Trim('0'));}}chevron_rightfilter_noneOutput: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
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | Uri.MakeRelativeUri(Uri) Method
- Array.GetValue() Method in C# with Examples | Set - 1
- C# | CharEnumerator.MoveNext() Method
- C# | Math.IEEERemainder() Method
- C# | Substring() Method
- C# | IndexOfAny() Method
- C# | CharEnumerator.GetType() Method
- File.GetLastWriteTimeUtc() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- Double.CompareTo Method in C# with Examples
- C# | Graphics.DrawLine() Method | Set - 1
- UInt16.GetHashCode Method in C# with Examples
- Stack.Peek Method in C#
- C# | IsNullOrEmpty() Method
- C# | Math.Sign() Method
- Int64.CompareTo Method in C# with Examples
- C# | PadRight() Method
- How to use Array.BinarySearch() Method in C# | Set -1
- C# | EndsWith() Method
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.

