Windows Dev Center

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.StartsWith Method (String, StringComparison)

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

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

'Declaration
<ComVisibleAttribute(False)> _
Public Function StartsWith ( _
	value As String, _
	comparisonType As StringComparison _
) As Boolean

Parameters

value
Type: System.String

The string to compare.

comparisonType
Type: System.StringComparison

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

Return Value

Type: System.Boolean
true if this instance begins with value; otherwise, false.

ExceptionCondition
ArgumentNullException

value is Nothing.

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, must be the empty string (""), or must match the beginning of this string. The type of comparison performed by the StartsWith method depends on the value of the comparisonType parameter. The comparison can use the conventions of the current culture (StringComparison.CurrentCulture and StringComparison.CurrentCultureIgnoreCase) or the invariant culture (StringComparison.InvariantCulture and StringComparison.InvariantCultureIgnoreCase), or it can consist of a character-by-character comparison of code points (StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase). The comparison can also be case-sensitive (StringComparison.CurrentCulture, StringComparison.InvariantCulture, or StringComparison.Ordinal), or it can ignore case (StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCase, StringComparison.OrdinalIgnoreCase).

The following example determines whether a string starts with a particular substring. It initializes a two-dimensional string array. The first element in the second dimension contains a string, and the second element contains the string to search for at the start of the first string. The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed. Note that when the string instance contains a ligature, culture-sensitive comparisons with its consecutive characters successfully match.

Module Example
   Public Sub Main()
      Dim strings(,) As String = { {"ABCdef", "abc" },                    
                                   {"ABCdef", "abc" },  
                                   {"œil","oe" },
                                   { "læring}", "lae" } }
      For ctr1 As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
            For Each cmpName As String In [Enum].GetNames(GetType(StringComparison)) 
               Dim strCmp As StringComparison = CType([Enum].Parse(GetType(StringComparison), 
                                                      cmpName), StringComparison)
               Dim instance As String = strings(ctr1, 0)
               Dim value As String = strings(ctr1, 1)
               Console.WriteLine("{0} starts with {1}: {2} ({3} comparison)", 
                                 instance, value, 
                                 instance.StartsWith(value, strCmp), 
                                 strCmp) 
            Next
            Console.WriteLine()   
      Next    
   End Sub 
End Module 
' The example displays the following output: 
'       ABCdef starts with abc: False (CurrentCulture comparison) 
'       ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison) 
'       ABCdef starts with abc: False (InvariantCulture comparison) 
'       ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison) 
'       ABCdef starts with abc: False (Ordinal comparison) 
'       ABCdef starts with abc: True (OrdinalIgnoreCase comparison) 
'        
'       ABCdef starts with abc: False (CurrentCulture comparison) 
'       ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison) 
'       ABCdef starts with abc: False (InvariantCulture comparison) 
'       ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison) 
'       ABCdef starts with abc: False (Ordinal comparison) 
'       ABCdef starts with abc: True (OrdinalIgnoreCase comparison) 
'        
'       œil starts with oe: True (CurrentCulture comparison) 
'       œil starts with oe: True (CurrentCultureIgnoreCase comparison) 
'       œil starts with oe: True (InvariantCulture comparison) 
'       œil starts with oe: True (InvariantCultureIgnoreCase comparison) 
'       œil starts with oe: False (Ordinal comparison) 
'       œil starts with oe: False (OrdinalIgnoreCase comparison) 
'        
'       læring} starts with lae: True (CurrentCulture comparison) 
'       læring} starts with lae: True (CurrentCultureIgnoreCase comparison) 
'       læring} starts with lae: True (InvariantCulture comparison) 
'       læring} starts with lae: True (InvariantCultureIgnoreCase comparison) 
'       læring} starts with lae: False (Ordinal comparison) 
'       læring} starts with lae: False (OrdinalIgnoreCase comparison)

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft