C# String Trim() Method
C# Trim() is a string class method. This method is used to remove all leading and trailing white-space characters from the current String object and return a new modified string. This method is beneficial for clearing the unwanted extra spaces from the string.
Example 1: Using the Trim() method to remove the leading and the trailing whitespaces from the string.
// C# program to demonstrate the trim method
using System;
class Geeks
{
public static void Main()
{
// Original string
String s = " GeeksForGeeks ";
// String before removing whitespaces
Console.WriteLine($"Original string: '{s}'");
// Applying Trim() method
string res = s.Trim();
// New string after removing whitespaces
Console.WriteLine($"After Trim() method: '{res}'");
}
}
Output
Original string: ' GeeksForGeeks ' After Trim() method: 'GeeksForGeeks'
Explanation: In the above example, we created a string which contains some extra leading and trailing white spaces which can be removed by the trim method as shown in the output if doesn't pass any argument it removes all trailing and leading spaces as shown in the output.
Syntax of String Trim() Method
This method can be overloaded by passing different arguments:
public string Trim()
public string Trim(char trimChar)
public string Trim (params char[] trimChars)
Parameters:
- trimChar(Optional): The specific character which we want to remove from the string.
- trimChars(Optional): It is the array of characters including the characters which we want to remove from the string either they are leading or trailing.
Return Type:
- It returns a new modified string the type of Trim() method is System.String.
Note: Null and White Space will not remove automatically if they are not specified in the arguments list.
Example: 2: Removing trailing and leading spaces from string using the Trim() method without passing any argument.
// C# program to illustrate the
// method without any parameters
using System;
class Geeks
{
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);
// After Trim method call
Console.WriteLine("After:");
Console.WriteLine(s1.Trim());
Console.WriteLine(s2.Trim());
Console.WriteLine(s3.Trim());
}
}
Output
Before: GFG GFG GFG After: GFG GFG GFG
Explanation: In the above example, 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. And returns a new modified string.
Example 3: Using the Trim() method with a single character as a parameter to remove trailing and leading characters from a string.
// C# program to illustrate trim(trimChar c) method
// with single character parameter
using System;
class Geeks
{
public static void Main()
{
// String initialization and Declaration
string s = "GeeksforGeeks";
// Before trim() method
Console.WriteLine("Before trim() method: " + s);
// After trim() method
Console.WriteLine("After trim() method: " + s.Trim('s'));
}
}
Output
Before trim() method: GeeksforGeeks After trim() method: GeeksforGeek
Explanation: In the above code example, we use the Trim(char trimChar) method which removes the trailing 's' from the given string GeeksforGeeks as shown in the output.
Example 4: Removing different leading and trailing characters from a string using the Trim(char[] trimChars) method with a character array as an argument.
// To illustrate the
// method with parameters
using System;
class Geeks
{
public static void Main()
{
// declare char[] array and
// initialize character 0 to 9
char[] chars = {'1', '2', '3', '4', '5',
'6', '7', '8', '9'};
string s1 = "123abc456xyz789";
Console.WriteLine("Before:" + s1);
Console.WriteLine("After:" + s1.Trim(chars));
Console.WriteLine("");
char[] char2 = { '*', '1', 'c' };
string s2 = "*123xyz********c******c";
Console.WriteLine("Before:" + s2);
Console.WriteLine("After:" + s2.Trim(char2));
Console.WriteLine("");
char[] chars3 = { 'G', 'e', 'k', 's' };
string s3 = "GeeksForGeeks";
Console.WriteLine("Before:" + s3);
Console.WriteLine("After:" + s3.Trim(chars3));
Console.WriteLine("");
string s4 = " Geeks0000";
Console.WriteLine("Before:" + s4);
Console.WriteLine("After:" + s4.Trim('0'));
}
}
Output
Before:123abc456xyz789 After:abc456xyz Before:*123xyz********c******c After:23xyz Before:GeeksForGeeks After:For Before: Geeks0000 After: Geeks
Explanation: In the above example, 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 is encountered. As shown in the current string is “123abc456xyz789” and trimChars contains the digits from “1 to 9”, then the Trim method returns “abc456xyz”.
Important Points of 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.
- If the current string equals Empty or all the characters in the current instance consist of white-space characters, the method returns Empty.