Open In App

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 :

Important Points About Trim() Method:

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


Article Tags :
C#