StartsWith Method (String, StringComparison)
Collapse the table of content
Expand the table of content

String.StartsWith Method (String, StringComparison)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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)

public bool StartsWith(
	string value,
	StringComparison comparisonType


Type: System.String
The string to compare.
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.


value is null.


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

using System;
using System.Threading;

class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      string intro = "Determine whether a string starts with another string, " +
                     "using\n  different values of StringComparison.";

      StringComparison[] scValues = {
        StringComparison.OrdinalIgnoreCase };

      outputBlock.Text += intro + "\n";

      // 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}.\n",
                         Thread.CurrentThread.CurrentCulture.Name) + "\n";

      // Determine whether three versions of the letter I are equal to each other. 
      foreach (StringComparison sc in scValues)
         outputBlock.Text += String.Format("StringComparison.{0}:", sc) + "\n";
         Test(outputBlock, "ABCxyz", "ABC", sc);
         Test(outputBlock, "ABCxyz", "abc", sc);
         outputBlock.Text += "\n";

   protected static void Test(System.Windows.Controls.TextBlock outputBlock, string x, string y, StringComparison comparison)
      string resultFmt = "\"{0}\" {1} with \"{2}\".";
      string result = "does not start";
      if (x.StartsWith(y, comparison))
         result = "starts";
      outputBlock.Text += String.Format(resultFmt, x, result, y) + "\n";

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.

"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2016 Microsoft