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)

[ComVisibleAttribute(false)]
public bool StartsWith(
	string value,
	StringComparison comparisonType
)

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 null.

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 searches for the string "the" at the beginning of a longer string that begins with the word "The". As the output from the example shows, a call to the StartsWith(String, StringComparison) method that performs a culture-insensitive but case-sensitive comparison fails to match the string, while a call that performs a culture- and case-insensitive comparison matches the string.

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.

using System;

public class Example
{
   public static void Main()
   {
      string[,] strings = { {"ABCdef", "abc" },                    
                            {"ABCdef", "abc" },  
                            {"œil","oe" },
                            { "læring}", "lae" } };
      for (int ctr1 = strings.GetLowerBound(0); ctr1 <= strings.GetUpperBound(0); ctr1++)
      {
            foreach (string cmpName in Enum.GetNames(typeof(StringComparison)))
            { 
               StringComparison strCmp = (StringComparison) Enum.Parse(typeof(StringComparison), 
                                                      cmpName);
               string instance = strings[ctr1, 0];
               string value = strings[ctr1, 1];
               Console.WriteLine("{0} starts with {1}: {2} ({3} comparison)", 
                                 instance, value, 
                                 instance.StartsWith(value, strCmp), 
                                 strCmp); 
            }
            Console.WriteLine();   
      }   
   }
}
// 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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft