
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# String - StartsWith() Method
The C# String StartsWith() method is used to check whether the beginning of a string matches a given substring. It provides multiple overloads to accommodate case sensitivity, culture-specific comparisons, and custom comparison rules.
Syntax
Following are the syntax of the C# string StartsWith(Char) method −
Basic Comparison (case-sensitive, current culture)
public string[] StartsWith(params char[] separator);
Case-Insensitive or Culture-Specific Comparison
public bool StartsWith(string value, bool ignoreCase, CultureInfo culture);
Custom Comparison Rules
public bool StartsWith(string value, StringComparison comparisonType);
Parameters
This method accepts the following parameters according to all overloaded syntax −
- value: It can be a string or character to compare.
- comparisonType: It is one of the enumeration values that determines how this string and value are compared.
- ignoreCase: It is a boolean that can be true to ignore a case during the comparison; otherwise, it is false.
- culture: It is the culture information that determines how this string and value are compared. If the culture is null, the current culture is used.
Return Value
This method returns boolean value, true
if value matches the beginning of this string; otherwise, false
.
Example 1: Basic Case-Sensitive Comparison:
Here is a basic example of the StartWith() method, where we compare case-sensitive words −
using System; class Program { public static void Main() { string text = "Hello, tpians!"; bool result = text.StartsWith("Hello"); Console.Write(result); } }
Output
Following is the output −
True
Example 2: Case-Insensitive Comparison
Let us look at another example. Here we use the StartWith() method with ignore-case to compare case-insensitive words −
using System; class Program { public static void Main() { string text = "Hello, tutorialspoint"; bool result = text.StartsWith("hello", StringComparison.OrdinalIgnoreCase); Console.Write(result); } }
Output
Following is the output −
True
Example 3: Culture-Specific Comparison
In this example, we use the StartsWith() method to compare a words with culture specific comparison −
using System; using System.Globalization; class Program { public static void Main() { // German word for "street" string text = "Strae"; bool result = text.StartsWith("Str", false, new CultureInfo("de-DE")); Console.Write(result); } }
Output
Following is the output −
True
Example 4: Turkish Culture-Specific Comparison
In this example, we use the StartsWith() method to compare the first character of a word with the Turkish culture-specific comparison −
using System; using System.Globalization; class Program { public static void Main() { // Turkish word for "tomorrow" string text = "stanbul"; // Comparing with "I" bool resultTurkish = text.StartsWith("I", false, new CultureInfo("tr-TR")); Console.WriteLine("Turkish culture comparison: " + resultTurkish); // Comparing with "" bool resultCorrect = text.StartsWith("", false, new CultureInfo("tr-TR")); Console.WriteLine("Correct Turkish comparison: " + resultCorrect); } }
Output
Following is the output −
Turkish culture comparison: False Correct Turkish comparison: True