C# String Trim(Char[])

Description

String Trim(Char[]) removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.

Syntax

String.Trim(Char[]) has the following syntax.


public string Trim(
  params char[] trimChars
)

Parameters

String.Trim(Char[]) has the following parameters.

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

Returns

String.Trim(Char[]) method 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.

Example

The following example uses the String.Trim(Char[]) method to remove space, asterisk (*), and apostrophe (') characters from a string.


using System;//w w w .  jav  a 2s.c  o m

public class Example
{
   public static void Main()
   {
      char[] charsToTrim = { '*', ' ', '\''};
      string banner = "*** this is a test ***";
      string result = banner.Trim(charsToTrim);
      Console.WriteLine(result);
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version