Advertisement



< Prev
Next >



C# StartsWith() method




As the name of the method goes, the StartsWith() method of String class determines if the value in a String object starts with a specified String or not.




Some overloads of StartsWith() method



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.






Example of StartsWith(String)


Let us explain the first overload version of StartsWith() method. In the upcoming code, we have a String object initialized to a value Hello World and we are going to check whether this String object starts with a specific String value or not.
//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);  
}
}


Output is :


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


Program Analysis






The StartsWith(String) performs a case-sensitive comparison.


By default, the first overload version of StartsWith() checks for comparison between String value passed to it and the invoked String object by conforming to the case sensitivity of each letter.

//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);  
}
}


Output is :


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


Program Analysis





Example of StartsWith(String, StringComparison)


In the upcoming code, we are going to explain the second overload of StartsWith() method i.e. StartsWith(String, StringComparison), which determines whether the beginning of the invoked String matches with String object passed to this method, when compared using a specified value of the StringComparison Enum.

The StringComparison is an Enum which specifies culture, sort, case rules to be used by whem the comparison is made between String objects. Its all important 6 fields are explained below.


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.



Note: The StartsWith(String, StringComparison) method also performs a case-sensitive search, unless you pass the a specific field of StringComparison with IgnoreCase specified in it.

//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);
}
}


Output is :


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




Example of StartsWith(String, Boolean, CultureInfo)


This version of StartsWith() method determines whether the beginning of the invoked String matches with String object passed to this method, when compared using specified CultureInfo.

To do this, we will call the constructor of the CultureInfo class that accepts a String(representing the culture) and a bool value that specifies whether to use the user-selected culture settings from the system or not.

//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));
}
}


Output is :


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

In our last example, we have made a Comparison between String object according to different cultures, by using an instance of CultureInfo class passed to a variant of the StartsWith() method.



Please share this article -




< Prev
Next >
< C# ToLower() Method
C# EndsWith() method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement