C# Tutorial C# Advanced C# References

C# String - StartsWith() Method



The C# StartsWith() method is used to check whether the string starts with specified value or not. It returns true if the string starts with the specified value, else returns false.

Note: This method can be overloaded by passing different type of arguments to it.

Syntax

public bool StartsWith(char value);
public bool StartsWith(string value);
public bool StartsWith(string value, StringComparison comparisonType);
public bool StartsWith(string value, bool ignoreCase, 
                       System.Globalization.CultureInfo culture);

Parameters

value Specify string or char to compare.
comparisonType Specify the StringComparison enumeration value.
ignoreCase Specify true to ignore case during comparison, false otherwise
culture Specify culture information that determines how the string and value is compared. If it is null, current culture is used.

Return Value

Returns true if the string starts with the specified value, else returns false.

Exception

  • Throws ArgumentNullException, if the value is null.
  • Throws ArgumentException, if the comparisonType is not a StringComparison value.

Example:

In the example below, StartsWith() method is used to check whether the string starts with specified value or not.

using System;
using System.Globalization;

class MyProgram {
  static void Main(string[] args) {
    string MyStr = "HELLO World";

    Console.WriteLine(MyStr.StartsWith("Hello"));

    Console.WriteLine(MyStr.StartsWith("Hello", 
          StringComparison.CurrentCultureIgnoreCase)); 

    Console.WriteLine(MyStr.StartsWith("Hello", true, 
          new CultureInfo("en-US"))); 
  }
}

The output of the above code will be:

False
True
True

❮ C# String Methods