GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C# String - StartsWith() Method
Selected Reading
C# String - StartsWith() MethodThe 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. SyntaxFollowing 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); ParametersThis method accepts the following parameters according to all overloaded syntax −
Return ValueThis method returns boolean value, 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);
}
}
OutputFollowing is the output − True Example 2: Case-Insensitive ComparisonLet 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);
}
}
OutputFollowing is the output − True Example 3: Culture-Specific ComparisonIn 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);
}
}
OutputFollowing is the output − True Example 4: Turkish Culture-Specific ComparisonIn 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);
}
}
OutputFollowing is the output − Turkish culture comparison: False Correct Turkish comparison: True csharp_strings.htm 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. |