In C#, StartsWith() is a string method. This method is used to check whether the beginning of the current string instance matches with a specified string or not. If it matches then it returns the string otherwise false. Using foreach-loop, it is possible to check many strings. This method can be overloaded by passing different type and number of arguments to it.
String.StartsWith(String) Method
This method is used to check whether the beginning of the string object matches with a particular string or not. If it matches then it returns the string otherwise return false.
Syntax:
public bool StartsWith(string input_string)
Parameter:
input_string: It is a required string which is to be compared and type of this parameter is System.String.
Return Type: This function returns the Boolean value i.e. true if it found a match, else it will return false. Return type is System.Boolean.
Below are the programs to demonstrates the use of String.StartsWith(String) method:
C#
using System;
public class GFG {
static public void Main()
{
string [] m = new string [] {
foreach ( string s in m)
{
if (str.StartsWith(s)) {
Console.WriteLine(s);
return ;
}
}
}
}
|
Output:
https://www.geeksforgeeks.org
C#
using System;
class Geeks {
public static void Main()
{
string [] input_str = {
"
<p>GeekforGeeks Computer Science Portal </p>
",
"<h1>GeekforGeeks Sudo Placement </h1>" ,
"<h2>GeekforGeeks Placement Preparation </h2>" ,
"<h3>GeekforGeeks Contribute </h3>" ,
"<h4>GeekforGeeks Contribute " ,
"<h5>GeekforGeeks Interview </h5>" ,};
foreach ( var n in input_str)
Console.WriteLine(htmlStartTags(n));
}
private static string htmlStartTags( string str)
{
if (str.Trim().StartsWith( "<" ))
{
int end = str.IndexOf( ">" );
if (end >= 0)
{
str = str.Substring(end + 1);
str = htmlStartTags(str);
}
}
return str;
}
}
|
Output:
GeekforGeeks Computer Science Portal </p>
GeekforGeeks Sudo Placement </h1>
GeekforGeeks Placement Preparation </h2>
GeekforGeeks Contribute </h3>
GeekforGeeks Contribute
GeekforGeeks Interview </h5>
Note:
- If the input_string is Null then this method will give ArgumentNullException.
- This method also performs a case-sensitive and culture-sensitive comparison by using the current culture.
String.StartsWith(String, Boolean, CultureInfo) Method
This method is used to check whether the beginning of the current string instance matches the specified string when it compared using the specified culture. If a match is found, then return the string otherwise return false.
Syntax:
public bool StartsWith(string str,
bool case,
CultureInfo cul)
Parameters:
str: It is the string which is to be compared and the type of this parameter is System.String.
case: It will set true to ignore case during the comparison, otherwise false and the type of this parameter is System.Boolean.
cul: It is the Cultural information which checks how current string and str are compared. If culture is null, the current culture is used and the type of this parameter is System.Globalization.CultureInfo.
Return Value: This function returns the value of type System.Boolean that evaluates true if the str matches with the beginning of current string else false.
Exception: If the value of str is null then this method will give ArgumentNullException.
Example:
C#
using System.Threading;
using System.Globalization;
using System;
class Sudo {
public static void Main( string [] args)
{
string str1 = "Geeks" ;
bool result_a = str1.StartsWith( "Geeks" , false ,
CultureInfo.InvariantCulture);
bool result_b = str1.StartsWith( "geeks" , false ,
CultureInfo.InvariantCulture);
bool result_c = str1.StartsWith( "GEEKS" , false ,
CultureInfo.InvariantCulture);
bool result_d = str1.StartsWith( " " , false ,
CultureInfo.InvariantCulture);
Console.WriteLine(result_a);
Console.WriteLine(result_b);
Console.WriteLine(result_c);
Console.WriteLine(result_d);
}
}
|
Output:
True
False
False
False
String.StartsWith(String, StringComparison) Method
This method is used to check whether the starting of the current string instance matches the specified string or not when compared using the specified comparison option. If a match is found, then it returns the string otherwise false.
Syntax:
bool StartsWith(String str, StringComparison cType)
Parameters:
str: It is the required string which is to be compared and type of this parameter is System.String.
cType: It is one of the enumeration values that determine how current string and str are compared. Type of this parameter is System.StringComparison.
Return Value: This function returns the Boolean value i.e. true if it found a match, else it will return false. Return type is System.Boolean.
Exceptions:
- If the value of str is null then this method will give ArgumentNullException.
- If the value of cType is not a StringComparison value then this method will give ArgumentException.
Example:
C#
using System;
class Sudo {
public static void Main( string [] args)
{
string str1 = "GeeksforGeeks" ;
string str2 = "Learn CSharp" ;
bool result_a = str1.StartsWith( "Geek" ,
StringComparison.CurrentCulture);
bool result_b = str1.StartsWith( "geek" ,
StringComparison.CurrentCulture);
bool result_tt = str2.StartsWith( "CSharp" ,
StringComparison.CurrentCulture);
bool result_t = str2.StartsWith( "Learn" ,
StringComparison.CurrentCulture);
Console.WriteLine(result_a);
Console.WriteLine(result_b);
Console.WriteLine(result_tt);
Console.WriteLine(result_t);
}
}
|
Output:
True
False
False
True
References:
Last Updated :
29 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...