C# String Trim()

Description

String Trim() removes all leading and trailing white-space characters from the current String object.

Syntax

String.Trim() has the following syntax.


public string Trim()

Returns

String.Trim() method returns The string that remains after all white-space characters are removed from the start and end of the current string.

Example

The following example uses the String.Trim() method to remove any extra white space from strings.


/*  w  w  w. ja  v a  2s. c  om*/
using System;

public class Example
{
   public static void Main()
   {
      string firstName = "  java2s.com   ";
      Console.WriteLine(firstName.Trim());
   }
}

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