C# String.StartsWith() – Examples

C# String.StartsWith() method is used to determine whether this string instance starts with the specified character or string.

In this tutorial, we will learn about the syntax and examples for different variations of C# String.StartsWith() method based on parameters.

  • String.StartsWith(ch)
  • String.StartsWith(str)
  • String.StartsWith(str, ignoreCase, culture)
  • String.StartsWith(str, comparisonType)

StartsWith(ch)

String.StartsWith(ch) determines whether this string instance starts with the specified character ch.

If given string starts with char, then String.StartsWith(char) returns True, else, the method returns False.

ADVERTISEMENT

Syntax

The syntax of StartsWith() method with character to compare as parameter is

String.StartsWith(Char ch)

where

ParameterDescription
chA character to compare the starting of this String instance with.

Return Value

This method returns boolean value.

Example 1 – StartsWith(ch)

In this example, we will take a string "abcd" and check if the string starts with the character 'a'.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String mystring = "abcd";
        Char ch = 'a';
        Boolean result = mystring.StartsWith(ch);
        Console.WriteLine($"Given string starts with specified char? {result}");
    }
}

Output

Given string starts with specified char? True

Example 2 – StartsWith(ch) – Negative Scenario

In this example, we will take a string "abcd" and check if the string starts with the character 'm'. Since the string does not start with specified character, String.StartWith() returns False.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String mystring = "abcd";
        Char ch = 'm';
        Boolean result = mystring.StartsWith(ch);
        Console.WriteLine($"Given string starts with specified char? {result}");
    }
}

Output

Given string starts with specified char? False

StartsWith(str)

String.StartsWith(str) determines whether the beginning of this string instance matches the specified string str. If there is a match, then the method returns True, else it returns False.

Syntax

The syntax of StartsWith() method with the substring as parameter is

String.StartsWith(String str)

where

ParameterDescription
strA string value to compare the starting of this String instance with.

Return Value

This method returns boolean value.

Example 3 – StartsWith(str)

In this example, we will take a string "abcd" and check if the string starts with the string "ab".

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String mystring = "abcd";
        String str = "ab";
        Boolean result = mystring.StartsWith(str);
        Console.WriteLine($"Given string starts with specified string? {result}");
    }
}

Output

Given string starts with specified string? True

StartsWith(str, ignoreCase, culture)

String.StartsWith(str, ignoreCase, culture) determines whether the beginning of this string instance matches the specified string str when compared using the specified culture culture. To ignore case, we can specify using ignoreCase Boolean parameter.

Syntax

The syntax of StartsWith() method with string to compare, ignore case flag, and Culture information as parameters is

String.StartsWith(String str, Boolean ignoreCase, CultureInfo culture)

where

ParameterDescription
strA string to compare the starting of this String instance with.
ignoreCaseIf true, case is ignored during comparison. If false, case is not ignored during comparison.
cultureCultural information that determines how this string instance and str are compared.

Return Value

This method returns boolean value.

Example 4 – StartsWith(str, ignoreCase, culture)

In this example, we will use StartsWith() method to check if the given string mystring starts with string str. We will tell StartsWith() to ignore the case while comparison using boolean parameter ignoreCase.

C# Program

using System;
using System.Globalization;
 
class Example {
    static void Main(string[] args) {
        String mystring = "abcd";
        String str = "AB";
        Boolean ignoreCase = true;
        CultureInfo culture = new CultureInfo("en-US");
        Boolean result = mystring.StartsWith(str, ignoreCase, culture);
        Console.WriteLine($"Given string starts with specified string? {result}");
    }
}

Output

Given string starts with specified string? True

StartsWith(str, comparisonType)

String.StartsWith(str, comparisonType) determines whether the beginning of this string instance matches the specified string str when compared using the specified comparison option comparisonType.

Syntax

The syntax of StartsWith() method with string to compare and comparison type as parameters is

String.StartsWith(String str, StringComparison comparisonType)

where

ParameterDescription
strThe string to compare.
comparisonTypeA value of type StringComparison that specifies how this String instance and str are compared.

Return Value

This method returns boolean value.

Example 5 – StartsWith(String, StringComparison)

In this example, we will take a string "abcd" and check if this string starts with "AB" and ignoring case while comparison.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String mystring = "abcd";
        String str = "AB";
        StringComparison comparisonType = StringComparison.OrdinalIgnoreCase;
        Boolean result = mystring.StartsWith(str, comparisonType);
        Console.WriteLine($"Given string starts with specified string? {result}");
    }
}

Output

Given string starts with specified string? True

Conclusion

In this C# Tutorial, we have learnt the syntax of C# String.StartsWith() method, and also learnt how to use this method with the help of C# example programs.