.NET Framework Class Library
String..::.Trim Method (array<Char>[]()[])

Updated: April 2009

Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
Public Function Trim ( _
    ParamArray trimChars As Char() _
) As String
Visual Basic (Usage)
Dim instance As String
Dim trimChars As Char()
Dim returnValue As String

returnValue = instance.Trim(trimChars)
C#
public string Trim(
    params char[] trimChars
)
Visual C++
public:
String^ Trim(
    ... array<wchar_t>^ trimChars
)
JScript
public function Trim(
    ... trimChars : char[]
) : String

Parameters

trimChars
Type: array<System..::.Char>[]()[]
An array of Unicode characters to remove or nullNothingnullptra null reference (Nothing in Visual Basic).

Return Value

Type: System..::.String
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 object. If trimChars is nullNothingnullptra null reference (Nothing in Visual Basic) or an empty array, white-space characters are removed instead.
Remarks

The Trim method removes from the current string all leading and trailing characters that are in the trimChars parameter. Each leading and trailing trim operation stops when a character that is not in trimChars is encountered. For example, if the current string is "123abc456xyz789" and trimChars contains the digits from '1' through '9', the Trim method returns "abc456xyz".

NoteNote:

This method does not modify the value of the current instance. Instead, it returns a new string in which all leading and trailing trimChars characters found in the current instance are removed.

If the current string equals Empty or all the characters in the current instance consist of characters in the trimChars array, the method returns [F:System.String.Empty.]

For more information about which Unicode characters are categorized as white-space characters, see the Remarks section of the String..::.Trim()()() method overload.

Examples

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

Visual Basic
Module Example
   Public Sub Main()
      Dim charsToTrim() As Char = { "*"c, " "c, "'"c}
      Dim banner As String = "*** Much Ado About Nothing ***"
      Dim result As String = banner.Trim(charsToTrim)
      Console.WriteLine("Trimmmed{0}   {1}{0}to{0}   '{2}'", _
                        vbCrLf, banner, result)
   End Sub
End Module
' The example displays the following output:
'       Trimmmed
'          *** Much Ado About Nothing ***
'       to
'          'Much Ado About Nothing'
C#
using System;

public class Example
{
   public static void Main()
   {
      char[] charsToTrim = { '*', ' ', '\''};
      string banner = "*** Much Ado About Nothing ***";
      string result = banner.Trim(charsToTrim);
      Console.WriteLine("Trimmmed\n   {0}\nto\n   '{1}'", banner, result);
   }
}
// The example displays the following output:
//       Trimmmed
//          *** Much Ado About Nothing ***
//       to
//          'Much Ado About Nothing'
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Change History

Date

History

Reason

April 2009

Expanded the Remarks section.

Customer feedback.

October 2008

Replaced the example.

Customer feedback.

Tags :


Community Content

knockNrod
How to trim a Public MyString As String in sockets
Trim, simply put, won't. At least, if it's coming from a C program on the other side. C inserts a null string terminator, and although CLR doesn't appear to use the null-terminator as a string terminator, it does recognize it as one. No matter how you copy the contents of your array of characters into native strings, it always brings with it the underlying character array. And Trim, even if you tell it to trim off the Nothings, simply refuses to remove the last null terminator. Printing such a string out will force the printed statement to terminate prematurely.

MyString = System.Text.Encoding.ASCII.GetString(buffer)
Debug.Print("MyString ({0}) has issues.", MyString)

will print something like this...

"MyString(<buffer contents>

The null terminator embedded in MyString forces the termination of the string, dropping ") has issues." or whatever else follows. Instead of using Trim(), use Substring(0, TempStringWithNullTerminator.IndexOf(Nothing)) to copy to a temporary string. (Nothing is the VB translation of the null terminator.) This trims off the buffer the first null-terminator and onward when you copy into the string.

Hope someone else finds this helpful. Took me half a workday to figure this out and I've seen others are struggling with this. I suspect this is actually a bug in the .Net Framework that was introduced from trying to support c-style strings.

Tags :

Page view tracker