ASP.Net C# String StartsWith Function

Updated on 18 May 2012,
Published on 05 Jul 2008

In ASP.Net C# string StartsWith Function is very useful that provides the functionality to check the starting characters of the string whether it starts with the specified pattern or not. C# StartsWith function returns the bool type value as true or false based on the result it evaluates after matching the string with specified string pattern. Additional parameter of overloaded functions of C# string StartsWith enables to match the string with case sensitive or insensitive approach as well as current culture. It matches the string pattern starting from the left most indexes of the provided string. Following are the overloads of ASP.Net 2.0 C# string StartsWith function:

// 1. 
public bool StartsWith(string value);

// 2. 
public bool StartsWith(string value, StringComparison comparisonType);

// 3. 
public bool StartsWith(string value, bool ignoreCase, CultureInfo culture);

String Functions Examples:

You can see the live samples and examples of String functions from the following links:

Above three overloads of the StartsWith string function accepts four different types of parameters. First parameter is a string type value that is to be matched with starting characters of the string. Second parameter type is StringComparison. It is an enumerator value that accepts the value from the following options:

  • CurrentCulture = 0,
  • CurrentCultureIgnoreCase = 1,
  • InvariantCulture = 2,
  • InvariantCultureIgnoreCase = 3,
  • Ordinal = 4,
  • OrdinalIgnoreCase = 5

Third parameter is bool type ignoreCase. If you will pass "true" then it will match the pattern by ignoring the string case whether it is in upper case or lower case does not matter. ignoreCase value as "false" matches the pattern as case sensitive. Last type of parameter is CultureInfo that matches the string based on the culture passed to the C# StartsWith string function.

Examples of ASP.Net C# String StartsWith function

// string StartsWith Function
string strText = "ASP.Net C# string.StartsWith function";
 
Response.Write(strText.StartsWith("ASP.Net") + "<br />");
 
Response.Write(strText.StartsWith("S") + "<br />");
 
Response.Write(strText.StartsWith("asp.net", StringComparison.CurrentCultureIgnoreCase) + "<br />");
 
Response.Write(strText.StartsWith("ASP.net", true, System.Globalization.CultureInfo.CurrentCulture) + "<br />");
Output

True

False

True

True

Output:

Learn with the help of live sample of C# StartsWith String Function from the following links:

String StartsWith Function 01

String StartsWith Function 02

Continue to next tutorial: ASP.Net C# String EndsWith Function to learn how to get whether the string ends with the specified string pattern/character or not.

0 Responses to "ASP.Net C# String StartsWith Function"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email