C# String StartsWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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.

C#
string url = "https://codetofun.com";

bool secure = url.StartsWith("https://", StringComparison.Ordinal);   // True
bool letter = url.StartsWith('h');                                     // True (newer .NET)
CallWhat it does
text.StartsWith("ab", StringComparison.Ordinal)True when text begins with exactly ab. Recommended.
text.StartsWith("AB", StringComparison.OrdinalIgnoreCase)Same test, ignoring upper and lower case.
text.StartsWith('a')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

ParameterDescription
valueRequired. The prefix to test. Must not be null.
comparisonTypeOptional. Case and culture rules, such as Ordinal or OrdinalIgnoreCase.
ignoreCase, cultureOptional pair. Ignore case using the rules of a specific culture.

Return Value

Return ValueDescription
trueThe string begins with value (or value is empty).
falseThe 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'));
    }
}

How It Works

  1. Exact prefix. The address begins with https://, so the first test is True.
  2. Close is not enough. http:// differs at its fifth character: the URL has an s where the prefix has :. The result is False.
  3. 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'.
  4. 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);
    }
}

How It Works

  1. Shouting user. The input begins with capital letters, while the prefix is lowercase.
  2. Ordinal is strict. 'H' and 'h' have different character codes, so the first test is False.
  3. OrdinalIgnoreCase relaxes it. Letters are compared without case, so HELP matches help and the result is True.
  4. 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);
            }
        }
    }
}

How It Works

  1. Four lines. Two are comments (with or without a space after #) and two are real settings.
  2. Negate the test. The ! flips the result, so the body runs only for lines that do not start with #.
  3. Prefix only. A # in the middle, such as color=#fff, would not be skipped, because StartsWith looks only at the beginning.
  4. 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.

Next: Substring()

Copy part of a string, from a start position and for an optional length.

Substring() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful