Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System
String Class
String Methods
StartsWith Method
 StartsWith Method (String, StringCo...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
String.StartsWith Method (String, StringComparison)

Note: This method is new in the .NET Framework version 2.0.

Determines whether the beginning of this string matches the specified string when compared using the specified comparison option.

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

Visual Basic (Declaration)
<ComVisibleAttribute(False)> _
Public Function StartsWith ( _
    value As String, _
    comparisonType As StringComparison _
) As Boolean
Visual Basic (Usage)
Dim instance As String
Dim value As String
Dim comparisonType As StringComparison
Dim returnValue As Boolean

returnValue = instance.StartsWith(value, comparisonType)
C#
[ComVisibleAttribute(false)] 
public bool StartsWith (
    string value,
    StringComparison comparisonType
)
C++
[ComVisibleAttribute(false)] 
public:
bool StartsWith (
    String^ value, 
    StringComparison comparisonType
)
J#
/** @attribute ComVisibleAttribute(false) */ 
public boolean StartsWith (
    String value, 
    StringComparison comparisonType
)
JScript
ComVisibleAttribute(false) 
public function StartsWith (
    value : String, 
    comparisonType : StringComparison
) : boolean

Parameters

value

A String object to compare to.

comparisonType

One of the StringComparison values that determines how this string and value are compared.

Return Value

true if the value parameter matches the beginning of this string; otherwise, false.
Exception typeCondition

ArgumentNullException

value is a null reference (Nothing in Visual Basic).

ArgumentException

comparisonType is not a StringComparison value.

The StartsWith method compares the value parameter to the substring at the beginning of this string and returns a value that indicates whether they are equal. To be equal, value must be a reference to this same string, be the empty string (""), or match the beginning of this string. The type of comparison performed by the StartsWith method depends on the value of the comparisonType parameter.

The following code example determines whether a string starts with a particular substring. The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed.

Visual Basic
' This example demonstrates the 
' System.String.StartsWith(String, StringComparison) method.

Imports System
Imports System.Threading

Class Sample
    Public Shared Sub Main() 
        Dim intro As String = "Determine whether a string starts with another string, " & _
                              "using" & vbCrLf & "  different values of StringComparison."
        
        Dim scValues As StringComparison() =  { _
                        StringComparison.CurrentCulture, _
                        StringComparison.CurrentCultureIgnoreCase, _
                        StringComparison.InvariantCulture, _
                        StringComparison.InvariantCultureIgnoreCase, _
                        StringComparison.Ordinal, _
                        StringComparison.OrdinalIgnoreCase }
        '
        Console.Clear()
        Console.WriteLine(intro)
        
        ' Display the current culture because the culture-specific comparisons
        ' can produce different results with different cultures.
        Console.WriteLine("The current culture is {0}." & vbCrLf, _
                           Thread.CurrentThread.CurrentCulture.Name)
        ' Determine whether three versions of the letter I are equal to each other. 
        Dim sc As StringComparison
        For Each sc In  scValues
            Console.WriteLine("StringComparison.{0}:", sc)
            Test("ABCxyz", "ABC", sc)
            Test("ABCxyz", "abc", sc)
            Console.WriteLine()
        Next sc
    
    End Sub 'Main

    Protected Shared Sub Test(ByVal x As String, ByVal y As String, _
                              ByVal comparison As StringComparison) 
        Dim resultFmt As String = """{0}"" {1} with ""{2}""."
        Dim result As String = "does not start"
        '
        If x.StartsWith(y, comparison) Then
            result = "starts"
        End If
        Console.WriteLine(resultFmt, x, result, y)
    
    End Sub 'Test
End Class 'Sample

'
'This code example produces the following results:
'
'Determine whether a string starts with another string, using
'  different values of StringComparison.
'The current culture is en-US.
'
'StringComparison.CurrentCulture:
'"ABCxyz" starts with "ABC".
'"ABCxyz" does not start with "abc".
'
'StringComparison.CurrentCultureIgnoreCase:
'"ABCxyz" starts with "ABC".
'"ABCxyz" starts with "abc".
'
'StringComparison.InvariantCulture:
'"ABCxyz" starts with "ABC".
'"ABCxyz" does not start with "abc".
'
'StringComparison.InvariantCultureIgnoreCase:
'"ABCxyz" starts with "ABC".
'"ABCxyz" starts with "abc".
'
'StringComparison.Ordinal:
'"ABCxyz" starts with "ABC".
'"ABCxyz" does not start with "abc".
'
'StringComparison.OrdinalIgnoreCase:
'"ABCxyz" starts with "ABC".
'"ABCxyz" starts with "abc".
'
C#
// This example demonstrates the 
// System.String.StartsWith(String, StringComparison) method.

using System;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
    string intro = "Determine whether a string starts with another string, " +
                   "using\n  different values of StringComparison.";

    StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

//
    Console.Clear();
    Console.WriteLine(intro);

// Display the current culture because the culture-specific comparisons
// can produce different results with different cultures.
    Console.WriteLine("The current culture is {0}.\n", 
                       Thread.CurrentThread.CurrentCulture.Name);

// Determine whether three versions of the letter I are equal to each other. 
    foreach (StringComparison sc in scValues)
        {
        Console.WriteLine("StringComparison.{0}:", sc);
        Test("ABCxyz", "ABC", sc);
        Test("ABCxyz", "abc", sc);
        Console.WriteLine();
        }
    }

    protected static void Test(string x, string y, StringComparison comparison)
    {
    string resultFmt = "\"{0}\" {1} with \"{2}\".";
    string result = "does not start";
//
    if (x.StartsWith(y, comparison))
        result = "starts";
    Console.WriteLine(resultFmt, x, result, y);
    }
}

/*
This code example produces the following results:

Determine whether a string starts with another string, using
  different values of StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.CurrentCultureIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

StringComparison.InvariantCulture:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.InvariantCultureIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

StringComparison.Ordinal:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.OrdinalIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

*/
C++
// This example demonstrates the
// System::String->StartsWith(String, StringComparison) method.

using namespace System;
using namespace System::Threading;

static void Test(String^ testString, String^ searchString,
    StringComparison comparison)
{
    String^ resultFormat = "\"{0}\" {1} with \"{2}\".";
    String^ resultString = "does not start";

    if (testString->StartsWith(searchString, comparison))
    {
        resultString = "starts";
    }
    Console::WriteLine(resultFormat, testString, resultString, searchString);
}

int main()
{
    String^ introMessage =
        "Determine whether a string starts with another string, " +
        "using\ndifferent values of StringComparison.";

    array<StringComparison>^ comparisonValues = {
        StringComparison::CurrentCulture, 
        StringComparison::CurrentCultureIgnoreCase, 
        StringComparison::InvariantCulture, 
        StringComparison::InvariantCultureIgnoreCase,
        StringComparison::Ordinal, StringComparison::OrdinalIgnoreCase};

    Console::Clear();
    Console::WriteLine(introMessage);

    // Display the current culture because the culture-specific comparisons
    // can produce different results with different cultures.
    Console::WriteLine("The current culture is {0}.\n",
        Thread::CurrentThread->CurrentCulture->Name);
    // Perform two tests for each StringComparison
    for each(StringComparison stringCmp in comparisonValues)
    {
        Console::WriteLine("StringComparison.{0}:", stringCmp);
        Test("ABCxyz", "ABC", stringCmp);
        Test("ABCxyz", "abc", stringCmp);
        Console::WriteLine();
    }
}

/*
This code example produces the following results:

Determine whether a string starts with another string, using
different values of StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.CurrentCultureIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

StringComparison.InvariantCulture:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.InvariantCultureIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

StringComparison.Ordinal:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.OrdinalIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

*/

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker