Last Updated : 11 Jul, 2025
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 :
// 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'));
}
}
Before: *****0000abc000**** abc -GFG- GeeksforGeeks After: abc000**** abc -GFG- forGeeks
// 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
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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4