C# String.Trim() – Examples
String.Trim() method is used to remove all leading and trailing white-space characters from the current string.
String.Trim(Char) method is used to remove specified character from leading and trailing edges of the current string.
String.Trim(Char[]) method is used to remove all specified characters from leading and trailing edges of the current string.
String.Trim() does not modify the original string but returns a new String instance with the trailing white-space/specified characters removed from the given string value.
In this tutorial, we will learn about the syntax of C# String.Trim() method, and learn how to use this method with the help of examples.
Trim()
String.Trim() removes all leading and trailing white-space characters from the current string.
Syntax
The syntax of Trim() method is
String.Trim()
Return Value
This method returns String value.
Example 1 – Trim()
In this example, we will take a string str with leading and trailing white spaces. To remove these leading and trailing white spaces, we will call Trim() method on this string str. Trim() returns a new String instance.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = " abc ";
String result = str.Trim();
Console.WriteLine($"Original String : \"{str}\"");
Console.WriteLine($"Result of Trim() : \"{result}\"");
}
}
Output
Original String : " abc "
Result of Trim() : "abc"
Example 2 – Trim() – No Leading or Trailing Spaces
In this example, we will take a string str with no leading and trailing white spaces. If we call Trim() method on this string str, Trim() returns the original String instance on which we are calling the method. In this case, Trim() does not create a new String.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "abc";
String result = str.Trim();
Console.WriteLine($"Original String : \"{str}\"");
Console.WriteLine($"Result of Trim() : \"{result}\"");
}
}
Output
Original String : "abc"
Result of Trim() : "abc"
Example 3 – Trim() – Null String
In we call Trim() method on a null String, then this method throws System.NullReferenceException.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = null;
String result = str.Trim();
Console.WriteLine($"Original String : \"{str}\"");
Console.WriteLine($"Result of Trim() : \"{result}\"");
}
}
Output
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Example.Main(String[] args) in D:\workspace\csharp\HelloWorld\Program.cs:line 7
Trim(Char)
String.Trim() removes all leading and trailing instances of a character from the current string.
Syntax
The syntax of Trim(Char) method is
String.Trim(Char trimChar)
where
| Parameter | Description |
|---|---|
| trimChar | A Unicode character to remove from leading and trailing edges of the String. |
Return Value
This method returns String value.
Example 4 – Trim(Char)
In this example, we will take a string, and trim the specified character if present at the edges of this string using Trim(Char) method.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "abcdeaaa";
Char trimChar = 'a';
String result = str.Trim(trimChar);
Console.WriteLine($"Original String : \"{str}\"");
Console.WriteLine($"Result of Trim() : \"{result}\"");
}
}
Output
Original String : "abcdeaaa"
Result of Trim() : "bcde"
Example 5 – Trim(Char) – Trailing Spaces
Trim(Char) does not remove white space characters. It considers only the trimChar we passed to this method for removal from the edges of the given string.
Note: You may specify space character as trimChar.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "abcdeaaa ";
Char trimChar = 'a';
String result = str.Trim(trimChar);
Console.WriteLine($"Original String : \"{str}\"");
Console.WriteLine($"Result of Trim() : \"{result}\"");
}
}
Output
Original String : "abcdeaaa "
Result of Trim() : "bcdeaaa "
Trim(Char[])
String.Trim() removes all leading and trailing occurrences of a set of characters specified in an array from the current string.
Syntax
The syntax of Trim(Char[]) method is
String.Trim(Char[] trimChars)
where
| Parameter | Description |
|---|---|
| trimChars | The array of Unicode characters to remove from leading and trailing edges of given string. |
Return Value
This method returns String value.
Example 6 – Trim(Char[])
In this example, we will take a string and remove all the leading and trailing characters that are in the specified character array trimChars, using Trim(Char[]) method.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "abcdeaafabab";
Char[] trimChars = {'a', 'b'};
String result = str.Trim(trimChars);
Console.WriteLine($"Original String : \"{str}\"");
Console.WriteLine($"Result of Trim() : \"{result}\"");
}
}
Output
Original String : "abcdeaafabab"
Result of Trim() : "cdeaaf"
Conclusion
In this C# Tutorial, we have learnt the syntax of C# String.Trim() method, and also learnt how to use this method with the help of C# example programs.
