.StartsWith()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Apr 27, 2023
Contribute to Docs

The .StartsWith() method determines whether the start of the string instance matches the specified character or string. Additional parameters may be supplied to define the criteria for a match.

Syntax

// Determines whether the String instance's first character matches the specified character.
String.StartsWith(char);

// Determines whether the start of String instance matches the specified string.
String.StartsWith(string);

// Same as above but uses additional case-sensitive and culture-sensitive criteria.
String.StartsWith(string, ignoreCase, culture)

// Same as above but uses an enumeration to determine the case- and culture-sensitive criteria.
String.StartsWith(string, comparisonType)

.StartsWith() is a static method of the String object. It takes the following arguments:

  • char is an instance of the Char structure; represents a single letter.
  • string is an instance of the String object.
  • ignoreCase is a boolean; if true then case is ignored for comparison (e.g., ‘a’ == ‘A’).
  • culture is an instance of the System.Globalization.CultureInfo class which includes culture-specific sort order rules.
  • comparisonType is an element of the StringComparison enumeration which encapsulates the case- and culture-specific criteria; available fields include:
    • CurrentCulture sets the current culture rules.
    • CurrentCultureIgnoreCase sets the current culture rules but ignores case.
    • InvariantCulture sets the invariant culture’s sorting rules (it’s a culture that doesn’t change based on the user’s location).
    • InvariantCultureIgnoreCase sets the invariant culture rules but ignores case.
    • Ordinal sets ordinal (binary) sort rules to compare strings.
    • OrdinalIgnoreCase sets ordinal (binary) sort rules to compare strings but ignores case.

Example

The following example illustrates a few of the ways the String.StartsWith() method may be used to evaluate the start of a string.

using System;
using System.Threading;
public class Example
{
public static void Main()
{
string baseString = "AbCdEfG";
string compareStart = "abc";
bool result;
// String.StartsWith(string)
result = baseString.StartsWith(compareStart);
Console.WriteLine("Simple compare: {0}", result.ToString());
// String.StartsWith(string, ignoreCase, culture)
result = baseString.StartsWith(compareStart, true, Thread.CurrentThread.CurrentCulture);
Console.WriteLine("Compare using Case/Culture: {0}", result.ToString());
// String.StartsWith(string, comparisonType)
result = baseString.StartsWith(compareStart, StringComparison.InvariantCulture);
Console.WriteLine("Compare using Enumeration: {0}", result.ToString());
}
}

This is what is printed to the console:

Simple compare: False
Compare using Case/Culture: True
Compare using Enumeration: False

Codebyte Example

In this example, the months array is populated with the months of the year, and the .StartsWith() method is used to determine the number of months that start in the letter ‘J’:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn C# on Codecademy