This documentation is archived and is not being maintained.

String.Trim Method ()

.NET Framework 1.1

Removes all occurrences of white space characters from the beginning and end of this instance.

[Visual Basic]
Overloads Public Function Trim() As String
[C#]
public string Trim();
[C++]
public: String* Trim();
[JScript]
public function Trim() : String;

Return Value

A new String equivalent to this instance after white space characters are removed from the beginning and end.

Remarks

For more information about what Unicode characters are categorized as white space characters, see the Remarks section of the Char.IsWhiteSpace method.

Example

[Visual Basic, C#, C++] The following example demonstrates the Trim method.

[Visual Basic] 
Imports System

Public Class TrimTest
    
    Public Shared Sub Main()
        Dim temp As String() = MakeArray()
        
        
        Console.WriteLine("Concatenating the inital values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)
        Dim i As Integer
        
        ' trim whitespace from both ends of the elements
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).Trim()
        
        Next i
        Console.WriteLine("Concatenating the trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)
        
        ' reset the array
        temp = MakeArray()
        
        ' trim the start of the elements. Passing null trims whitespace only
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).TrimStart(" "c)
        Next i

        Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)

        ' reset the array
        temp = MakeArray()

        ' trim the end of the elements. Passing null trims whitespace only
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).TrimEnd(" "c)
        Next i

        Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'", [String].Concat(temp))
    End Sub 'Main

    Private Shared Function MakeArray() As String()
        Dim arr As String() = {"  please    ", "  tell    ", "  me    ", "  about    ", "  yourself    "}
        Return arr
    End Function 'MakeArray
End Class 'TrimTest

[C#] 
using System;

public class TrimTest {
    public static void Main() {

        string [] temp = MakeArray();

        Console.WriteLine("Concatenating the inital values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // trim whitespace from both ends of the elements
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].Trim();

        Console.WriteLine("Concatenating the trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

        // trim the start of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimStart(null);

        Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

        // trim the end of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimEnd(null);

        Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'", String.Concat(temp));
    }

    private static string [] MakeArray() {
        string [] arr = {"  please    ", "  tell    ", "  me    ", "  about    ", "  yourself    "};
        return arr;
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

String* MakeArray()[]
{
   String* arr[] = {S"  please    ", S"  tell    ", S"  me    ", S"  about    ", S"  yourself    "};
   return arr;
}

int main()
{

   String* temp[] = MakeArray();

   Console::WriteLine(S"Concatenating the inital values in the array, we get the string:");
   Console::WriteLine(S"'{0}'\n", String::Concat(temp));

   // trim whitespace from both ends of the elements
   for (int i = 0; i < temp->Length; i++)
      temp->Item[i] = temp[i]->Trim();

   Console::WriteLine(S"Concatenating the trimmed values in the array, we get the string:");
   Console::WriteLine(S"'{0}'", String::Concat(temp));

   // reset the array
   temp = MakeArray();

   // trim the start of the elements-> Passing 0 trims whitespace only
   for (int i = 0; i < temp->Length; i++)
      temp->Item[i] = temp[i]->TrimStart(0);

   Console::WriteLine(S"Concatenating the start-trimmed values in the array, we get the string:");
   Console::WriteLine(S"'{0}'\n", String::Concat(temp));

   // reset the array
   temp = MakeArray();

   // trim the end of the elements-> Passing 0 trims whitespace only
   for (int i = 0; i < temp->Length; i++)
      temp->Item[i] = temp[i]->TrimEnd(0);

   Console::WriteLine(S"Concatenating the end-trimmed values in the array, we get the string:");
   Console::WriteLine(S"'{0}'", String::Concat(temp));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | String.Trim Overload List | TrimEnd | TrimStart

Show: