Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Trim Method

Removes all occurrences of a set of specified characters from the beginning and end of this instance.

Overload List

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

Supported by the .NET Compact Framework.

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

Removes all occurrences of a set of characters specified in an array from the beginning and end of this instance.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Trim(ParamArray Char()) As String
[C#] public string Trim(params char[]);
[C++] public: String* Trim(__wchar_t __gc[]);
[JScript] public function Trim(Char[]) : String;

Example

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

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Trim. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
 _

Class stringTrim2
   
   Public Shared Sub Main()
      Dim str1 As [String] = "*;|@123***456@|;*"
      Dim delim As [String] = "*;|@"
      Dim str2 As [String] = str1.Trim(delim.ToCharArray())
      
      Console.WriteLine("Delimiters:      {0}", delim)
      Console.WriteLine("Original string: {0}", str1)
      Console.WriteLine("Trimmed string:  {0}", str2)
   End Sub 'Main
End Class 'stringTrim2

[C#] 
using System;

class stringTrim2 {
    public static void Main() {
        String str1 = "*;|@123***456@|;*";
    String delim = "*;|@";
    String str2 = str1.Trim(delim.ToCharArray());

    Console.WriteLine("Delimiters:      {0}", delim);
    Console.WriteLine("Original string: {0}", str1);
    Console.WriteLine("Trimmed string:  {0}", str2);
    }
}

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

using namespace System;

int main()
{
   String* str1 = S"*;|@123***456@|;";
   String* delim = S"*;|@";
   String* str2 = str1->Trim(delim->ToCharArray());

   Console::WriteLine(S"Delimiters:      {0}", delim);
   Console::WriteLine(S"Original string: {0}", str1);
   Console::WriteLine(S"Trimmed string:  {0}", str2);
}

[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.

See Also

String Class | String Members | System Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft