C# .StartsWith()
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:
charis an instance of theCharstructure; represents a single letter.stringis an instance of theStringobject.ignoreCaseis a boolean; iftruethen case is ignored for comparison (e.g., ‘a’ == ‘A’).cultureis an instance of theSystem.Globalization.CultureInfoclass which includes culture-specific sort order rules.comparisonTypeis an element of theStringComparisonenumeration which encapsulates the case- and culture-specific criteria; available fields include:CurrentCulturesets the current culture rules.CurrentCultureIgnoreCasesets the current culture rules but ignores case.InvariantCulturesets the invariant culture’s sorting rules (it’s a culture that doesn’t change based on the user’s location).InvariantCultureIgnoreCasesets the invariant culture rules but ignores case.Ordinalsets ordinal (binary) sort rules to compare strings.OrdinalIgnoreCasesets 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: FalseCompare using Case/Culture: TrueCompare 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’:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
- Beginner Friendly.15 hours