GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — C# String - StartsWith() Method

Csharp Tutorial
  • 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# - Switch Expressions
  • C# Control Statements
  • C# - Loops
  • C# - For Loop
  • C# - While Loop
  • C# - Do While Loop
  • C# - Nested Loops
  • C# - Break
  • C# - Continue
  • C# - Foreach Loop
  • C# - Goto Statement
  • 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# - Custom Exceptions
  • 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# - LINQ
  • C# - IEnumerable vs IEnumerator
  • C# - Anonymous Methods
  • C# - Unsafe Codes
  • C# - Tasks and Parallel Programming
  • C# - Multithreading
  • C# - Extension Methods
  • C# - Lambda Expressions
  • C# - Async and Await
  • C# Modern Features
  • C# - Tuples
  • C# - Records
  • C# - Pattern Matching Enhancements
  • C# - Top-level Statements
  • C# - Nullable Reference Types
  • C# - What's New in C# 11 / 12 / 13
  • C# - Global Usings
  • C# - File-Scoped Namespaces
  • C# Practical & Advanced Usage
  • C# - JSON & XML Handling
  • C# - Data Serialization & Deserialization
  • C# - REST API Calls with Httpclient
  • C# - Dependency Injection
  • C# - Unit Testing with NUnit, xUnit & MSTest
  • C# - Package Management with NuGet
  • C# Useful Resources
  • C# - Developer's AI Tools
  • C# - Questions and Answers
  • C# - Cheatsheet
  • C# - Quick Guide
  • C# - Useful Resources
  • C# - Discussion
  • C# - Online Compiler
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

C# String - StartsWith() Method



Previous
Quiz
Next

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
csharp_strings.htm
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.