In C#, StartsWith() checks whether a string begins with some text. It returns true or false. It is the mirror of EndsWith(), which checks the end.
Use it for prefixes: a URL that starts with https, a command that starts with /, or a line that starts with #. By default the check is case-sensitive.
This is an instance member of System.String. Call it on a string variable, such as text.StartsWith(...). Calling it on a null variable throws NullReferenceException. It only reads the text. Strings are immutable, so the original string is never changed.
Syntax
Call it on the string you test, and pass the prefix.
Checks only the first character (newer .NET, .NET Core 2.0 and later).
text.StartsWith("ab")
Uses the current culture rules. Fine for simple words, but prefer an explicit comparison.
An empty prefix "" always returns true. A null prefix throws ArgumentNullException. Like IndexOf(string), the plain one-argument string overload is culture-sensitive, so pass StringComparison.Ordinal for codes, file names, and protocols.
Library declarations
C#
public bool StartsWith(string value);
public bool StartsWith(string value, StringComparison comparisonType);
public bool StartsWith(string value, bool ignoreCase, CultureInfo culture);
public bool StartsWith(char value); // newer .NET
Parameters
Parameter
Description
value
Required. The prefix to test. Must not be null.
comparisonType
Optional. Case and culture rules, such as Ordinal or OrdinalIgnoreCase.
ignoreCase, culture
Optional pair. Ignore case using the rules of a specific culture.
Return Value
Return Value
Description
true
The string begins with value (or value is empty).
false
The string does not begin with value.
C# Examples
Example 1 — Is This URL Secure?
Test the same address against two prefixes, and check its first letter with the char overload.
C#
using System;
class Program {
static void Main() {
string url = "https://codetofun.com";
Console.WriteLine(url.StartsWith("https://", StringComparison.Ordinal));
Console.WriteLine(url.StartsWith("http://", StringComparison.Ordinal));
Console.WriteLine(url.StartsWith('h'));
}
}
Output
True
False
True
How It Works
Exact prefix. The address begins with https://, so the first test is True.
Close is not enough.http:// differs at its fifth character: the URL has an s where the prefix has :. The result is False.
One character.StartsWith('h') looks only at index 0. This overload exists in newer .NET; on older versions use url.Length > 0 && url[0] == 'h'.
Read-only test. Nothing is cut or copied; you just get a bool back.
Example 2 — Accept a Command in Any Case
A user types HELP me. A case-sensitive test misses it; an ignore-case test catches it.
C#
using System;
class Program {
static void Main() {
string input = "HELP me";
bool exact = input.StartsWith("help", StringComparison.Ordinal);
bool relaxed = input.StartsWith("help", StringComparison.OrdinalIgnoreCase);
Console.WriteLine(exact);
Console.WriteLine(relaxed);
}
}
Output
False
True
How It Works
Shouting user. The input begins with capital letters, while the prefix is lowercase.
Ordinal is strict.'H' and 'h' have different character codes, so the first test is False.
OrdinalIgnoreCase relaxes it. Letters are compared without case, so HELP matches help and the result is True.
No copies made. You do not need input.ToLower() first; passing the comparison is shorter and avoids creating a new string.
Example 3 — Skip Comment Lines in a Config File
Print only the settings from a small config, skipping every line that starts with #.
C#
using System;
class Program {
static void Main() {
string[] lines = { "# settings", "name=Sam", "#todo", "age=30" };
foreach (string line in lines) {
if (!line.StartsWith("#", StringComparison.Ordinal)) {
Console.WriteLine(line);
}
}
}
}
Output
name=Sam
age=30
How It Works
Four lines. Two are comments (with or without a space after #) and two are real settings.
Negate the test. The ! flips the result, so the body runs only for lines that do not start with #.
Prefix only. A # in the middle, such as color=#fff, would not be skipped, because StartsWith looks only at the beginning.
Original order. The surviving lines print in the same order as the array: name=Sam, then age=30.
Frequently Asked Questions
It returns true when the string begins with the text you pass, and false otherwise. It does not change the string.
Yes, by default. Pass StringComparison.OrdinalIgnoreCase to ignore upper and lower case.
The one-argument string overload uses the current culture, which can vary between machines. Ordinal compares exact character codes and is the right choice for URLs, file names, and codes.
It returns true. Every string begins with the empty string. Passing null throws ArgumentNullException.
Yes, in newer .NET (.NET Core 2.0 and later). On .NET Framework, test text.Length > 0 && text[0] == c instead.