Export (0) Print
Expand All

String.StartsWith Method (String, StringComparison)

Updated: October 2010

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)> _
<SecuritySafeCriticalAttribute> _
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 the value parameter matches the beginning of this string; 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).

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

The StartsWith(String, StringComparison) method returns incorrect values when the string contains Unicode characters.

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.


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

Imports System.Threading

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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}

      outputBlock.Text &= intro & vbCrLf

      ' Display the current culture because the culture-specific comparisons
      ' can produce different results with different cultures.
      outputBlock.Text &= String.Format("The current culture is {0}." & vbCrLf, _
                         Thread.CurrentThread.CurrentCulture.Name) & vbCrLf
      ' Determine whether three versions of the letter I are equal to each other. 
      Dim sc As StringComparison
      For Each sc In scValues
         outputBlock.Text &= String.Format("StringComparison.{0}:", sc) & vbCrLf
         Test(outputBlock, "ABCxyz", "ABC", sc)
         Test(outputBlock, "ABCxyz", "abc", sc)
         outputBlock.Text &= vbCrLf
      Next sc

   End Sub 'Main

   Protected Shared Sub Test(ByVal outputBlock As System.Windows.Controls.TextBlock, 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
      outputBlock.Text &= String.Format(resultFmt, x, result, y) & vbCrLf

   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".
'


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Date

History

Reason

October 2010

Expanded the Remarks section and replaced the example.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft