Export (0) Print
Expand All

String.StartsWith Method (String, StringComparison)

Updated: August 2010

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)

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

Parameters

value
Type: System.String

A String object to compare to.

comparisonType
Type: System.StringComparison

One of the StringComparison 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 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, 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 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.

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)

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

August 2010

Expanded the Remarks section and replaced the example.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft