Windows apps
Collapse the table of content
Expand the table of content
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 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
/** @attribute ComVisibleAttribute(false) */ 
public boolean StartsWith (
	String value, 
	StringComparison comparisonType
public function StartsWith (
	value : String, 
	comparisonType : StringComparison
) : boolean
Not applicable.



A String object to compare to.


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


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


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.

// 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.OrdinalIgnoreCase };


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

// 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);

    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.

"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 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0
© 2018 Microsoft