Export (0) Print
Expand All

String.TrimStart Method

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

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

public string TrimStart (
	params char[] trimChars
)
public String TrimStart (
	char[] trimChars
)
public function TrimStart (
	... trimChars : char[]
) : String
Not applicable.

Parameters

trimChars

An array of Unicode characters to remove or a null reference (Nothing in Visual Basic).

Return Value

The string that remains after all occurrences of characters in the trimChars parameter are removed from the start of the current String object. If trimChars is a null reference (Nothing in Visual Basic), white-space characters are removed instead.

The TrimStart method removes from the current string all leading characters that are in the trimChars parameter. The 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 TrimStart method returns "abc456xyz789".

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

The following example uses the TrimStart method to trim white space and comment characters from lines of source code. The StripComments method wraps a call to TrimStart and passes it a character array that contains a space and the comment character (an apostrophe ( ' ) in Visual Basic and a slash ( / ) in C#). The TrimStart method is also called to remove leading white space when evaluating whether a string is a comment.

using System;


namespace String_Example
{
    class Application
    {
        public static void Main()
        {
            // Create a string that will be trimmed.
            string path = "c:/temp/";

            // Create an array of characters 
            // that represent characters to trim.
            char[] charsToTrim = {'c', ':' };

            // Trim the string.
            string trimmedPath = path.TrimStart(charsToTrim);

            Console.WriteLine("The trimmed value is: {0}.", trimmedPath);

            // Create a string that will be trimmed.
            string pathWhitespace = "                        c:/temp/";

            // Trim whitespaces by passing null.
            string trimmedWhiteSpace = pathWhitespace.TrimStart(null);

            Console.WriteLine("The trimmed value is: {0}.", trimmedWhiteSpace);
        }

    }
}
// This code example displays the following
// to the console:
//
// The trimmed value is: /temp/.
// The trimmed value is: c:/temp/.

The following example then illustrates a call to the StripComments method.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft