Advertisement
Methods | Description |
---|---|
StartsWith(String) | This method determines whether the beginning of the invoked String matches with String object passed to this method. |
StartsWith(String, StringComparison) | This method determines whether the beginning of the invoked String matches with String object passed to this method, when compared using specified StringComparison. |
StartsWith(String, Boolean, CultureInfo) | This method determines whether the beginning of the invoked String matches with String object passed to this method, when compared using specified CultureInfo. |
//C# StartsWith() method of String.
using System;
class StringStartsWith
{
public static void Main()
{
String str1= "Hello World";
Console.WriteLine("String is " + str1);
bool boo1 = str1.StartsWith("He");
Console.WriteLine("Does "+ str1 + " starts with He ? "+ boo1);
boo1 = str1.StartsWith("Hello");
Console.WriteLine("Does "+ str1 + " starts with Hello ? "+ boo1);
boo1= str1.StartsWith("Hey");
Console.WriteLine("Does "+ str1 + " starts with Hey ? "+ boo1);
boo1= str1.StartsWith("Hellow");
Console.WriteLine("Does "+ str1 + " starts with Hellow ? "+ boo1);
boo1= str1.StartsWith("World");
Console.WriteLine("Does "+ str1 + " starts with World ? "+ boo1);
}
}
String is Hello World
Does Hello World starts with He ? True
Does Hello World starts with Hello ? True
Does Hello World starts with Hey ? False
Does Hello World starts with Hellow ? False
Does Hello World starts with World ? False
//C# Example of StartsWith(String) method of String.
using System;
class StringStartsWith
{
public static void Main()
{
String str1 = "World Peace";
Console.WriteLine("String is: " + str1);
bool boo1 = str1.StartsWith("World");
Console.WriteLine("Does "+ str1 + " starts with World ? "+ boo1);
boo1 = str1.StartsWith("world");
Console.WriteLine("Does "+ str1 + " starts with world ? "+ boo1);
boo1 = str1.StartsWith("WORLD");
Console.WriteLine("Does "+ str1 + " starts with WORLD ? "+ boo1);
}
}
String is: World Peace
Does World Peace starts with World ? True
Does World Peace starts with world ? False
Does World Peace starts with WORLD ? False
Fields of StringComparison | Value | Description |
---|---|---|
CurrentCulture | 0 | Compares String objects using culture-sensitive sort rules and current culture. |
CurrentCultureIgnoreCase | 1 | Compares String objects using culture-sensitive sort rules and current culture, while ignoring the case of the String objects being compared. |
InvariantCulture | 2 | Compares String objects using culture-sensitive sort rules and invariant culture. |
InvariantCultureIgnoreCase | 3 | Compares String objects using culture-sensitive sort rules and invariant culture, while ignoring the case of the String objects being compared. |
Ordinal | 4 | Compares String objects using ordinal(binary) sort rules. |
OrdinalIgnoreCase | 5 | Compares String objects using ordinal(binary) sort rules, while ignoring the case of the String objects being compared. |
//C# Example of StartsWith(String, StringComparison) method of String.
using System;
class StringStartsWith
{
public static void Main()
{
String str1= "Hello World";
Console.WriteLine("String is " + str1);
//String comparison made using StringComparison.CurrentCulture
Console.WriteLine("String comparison by StringComparison.CurrentCulture:");
bool boo1 = str1.StartsWith("Hello", StringComparison.CurrentCulture);
Console.WriteLine("Does "+ str1 + " starts with Hello ? "+ boo1);
boo1 = str1.StartsWith("hello", StringComparison.CurrentCulture);
Console.WriteLine("Does "+ str1 + " starts with hello ? "+ boo1);
//String comparison made using StringComparison.CurrentCultureIgnoreCase
Console.WriteLine("String comparison by StringComparison.CurrentCultureIgnoreCase:");
boo1 = str1.StartsWith("HELLO", StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine("Does "+ str1 + " starts with HELLO ? "+ boo1);
boo1 = str1.StartsWith("hellow", StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine("Does "+ str1 + " starts with hellow ? "+ boo1);
//String comparison made using StringComparison.InvariantCulture
Console.WriteLine("String comparison by StringComparison.InvariantCulture: ");
boo1 = str1.StartsWith("Hello", StringComparison.InvariantCulture);
Console.WriteLine("Does "+ str1 + " starts with Hello ? "+ boo1);
boo1 = str1.StartsWith("hello", StringComparison.InvariantCulture);
Console.WriteLine("Does "+ str1 + " starts with hello ? "+ boo1);
//String comparison made using StringComparison.InvariantCultureIgnoreCase
Console.WriteLine("String comparison by StringComparison.InvariantCultureIgnoreCase: ");
boo1 = str1.StartsWith("HELLO", StringComparison.InvariantCultureIgnoreCase);
Console.WriteLine("Does "+ str1 + " starts with HELLO ? "+ boo1);
boo1 = str1.StartsWith("hellow", StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine("Does "+ str1 + " starts with hellow ? "+ boo1);
//String comparison made using StringComparison.Ordinal
Console.WriteLine("String comparison by StringComparison.Ordinal: ");
boo1 = str1.StartsWith("Hello", StringComparison.Ordinal);
Console.WriteLine("Does "+ str1 + " starts with Hello ? "+ boo1);
boo1 = str1.StartsWith("hello", StringComparison.Ordinal);
Console.WriteLine("Does "+ str1 + " starts with hello ? "+ boo1);
//String comparison made using StringComparison.OrdinalIgnoreCase
Console.WriteLine("String comparison by StringComparison.OrdinalIgnoreCase: ");
boo1 = str1.StartsWith("HELLO", StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Does "+ str1 + " starts with HELLO ? "+ boo1);
boo1 = str1.StartsWith("hellow", StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine("Does "+ str1 + " starts with hellow ? "+ boo1);
}
}
String is Hello World
String comparison by StringComparison.CurrentCulture:
Does Hello World starts with Hello ? True
Does Hello World starts with hello ? False
String comparison by StringComparison.CurrentCultureIgnoreCase:
Does Hello World starts with HELLO ? True
Does Hello World starts with hellow ? False
String comparison by StringComparison.InvariantCulture:
Does Hello World starts with Hello ? True
Does Hello World starts with hello ? False
String comparison by StringComparison.InvariantCultureIgnoreCase:
Does Hello World starts with HELLO ? True
Does Hello World starts with hellow ? False
String comparison by StringComparison.Ordinal:
Does Hello World starts with Hello ? True
Does Hello World starts with hello ? False
String comparison by StringComparison.OrdinalIgnoreCase:
Does Hello World starts with HELLO ? True
Does Hello World starts with hellow ? False
Advertisement
//C# Example of StartsWith(String, Boolean, CultureInfo)
using System;
using System.Globalization;
class StringUpper
{
public static void Main()
{
//Extracting and printing the current culture of our system
CultureInfo current = CultureInfo.CurrentUICulture;
Console.WriteLine("The current UI culture of our system is {0}", current.Name);
//Creating a String object and printing it
String str1 = "Share Smiles";
Console.WriteLine("String is: " + str1);
//Creating an object of CultureInfo class based on the English-US culture
CultureInfo ci1 = new CultureInfo("en-us", false);
//Creating an object of CultureInfo class based on the Turkish-Turkey culture
CultureInfo ci2 = new CultureInfo("tk-TR", false);
//Creating an object of CultureInfo class based on the Turkish-Turkey culture
CultureInfo ci3 = new CultureInfo("fr-FR", false);
//Comparing String objects according to English-United States culture
Console.WriteLine("Based on English-US culture, does " + str1 + " starts with Share ? " + str1.StartsWith("Share", false, ci1));
//Comparing String objects according to Turkish-Turkey culture
Console.WriteLine("based on Turkish-Turkish culture, does " + str1 + " starts with Share ? " + str1.StartsWith("Share", false, ci2));
//Comparing String objects according to French-France culture
Console.WriteLine("Based on French-France culture, does " + str1 + " starts with Share ?" + str1.StartsWith("Share", false, ci3));
}
}
The current UI culture of our system is en-US
String is: Share Smiles
Based on English-US culture, does Share Smiles starts with Share ? True
based on Turkish-Turkish culture, does Share Smiles starts with Share ? True
Based on French-France culture, does Share Smiles starts with Share ?True
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement