System.String.Trim Method

Removes all leading and trailing occurrences of a set of characters specified in an array from the current string object.

Syntax

public string Trim (params char[] trimChars)

Parameters

trimChars
An array of Unicode characters to remove, or null.

Returns

The string that remains after all occurrences of the characters in the trimChars parameter are removed from the start and end of the current string. If trimChars is null or an empty array, white-space characters are removed instead.

Remarks

The string.Trim(Char[]) method removes from the current string all leading and trailing characters that are in the trimChars parameter. Each leading and trailing trim operation stops when a character that is not in trimChars is encountered. For example, if the current string is "123abc456xyz789" and trimChars contains the digits from "1" through "9", the string.Trim(Char[]) method returns "abc456xyz".

Note:

This method does not modify the value of the current instance. Instead, it returns a new string in which all leading and trailing trimChars characters found in the current instance are removed.

If the current string equals string.Empty or all the characters in the current instance consist of characters in the trimChars array, the method returns string.Empty.

If trimChars is null or an empty array, this method removes any leading or trailing characters that result in the method returning true when they are passed to the char.IsWhiteSpace(char) method,

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0