Use the Trim(), TrimStart(), and TrimEnd() methods to trim strings in CSharp

Description

The following code shows how to use the Trim(), TrimStart(), and TrimEnd() methods to trim strings.

Example


  //  w  w  w.j  a  v  a 2s .co  m
  
  
    

using System;

class MainClass {

    public static void Main() {

        string myString18 = '(' +"  Whitespace  ".Trim() + ')';
        Console.WriteLine("'(' +\"  Whitespace  \".Trim() + ')' = " + myString18);
        string myString19 = '(' + "  Whitespace  ".TrimStart() + ')';
        Console.WriteLine("'(' +\"  Whitespace  \".TrimStart() + ')' = " + myString19);
        string myString20 = '(' + "  Whitespace  ".TrimEnd() + ')';
        Console.WriteLine("'(' +\"  Whitespace  \".TrimEnd() + ')' = " + myString20);

    
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var