Skip to content
Related Articles

Related Articles

Improve Article

C# | StartsWith() Method

  • Last Updated : 23 Jul, 2021

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: 
 



  • Program 1:
     

CSHARP




// C# program to illustrate the
// String.StartsWith(String) Method
using System;
 
public class GFG {
 
// Main Method
static public void Main()
{
 
    // The input string or character
 
    // Different string character and
    // Possible string to be matches
    string[] m = new string[] {
            "https://geeksforgeeks.org",
            "https://www.geeksforgeeks.org",
            "https://www.geeksforgeeks.org/placement"};
 
    // Using foreach - loop to check
    // each possible match
    foreach(string s in m)
    {
         
        // To check match second possibility
        if (str.StartsWith(s)) {
             
            // Display the result
            Console.WriteLine(s);
            return;
        }
    }
}
}
  • Output: 
     
https://www.geeksforgeeks.org
  • Program 2: 
     

CSHARP




// C# program to illustrate the
// String.StartsWith (String) Method
using System;
 
class Geeks {
 
    // Main Method
    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>",};
 
        // Display result after implementation StartsWith()
        // method in strings starting to be tags removed.
        foreach(var n in input_str)
 
            Console.WriteLine(htmlStartTags(n));
    }
 
    private static string htmlStartTags(string str)
    {
         
        // To check starting "<"tag is or not
        if (str.Trim().StartsWith("<"))
        {
 
            // Find the closing ">" tag.
            int end = str.IndexOf(">");
 
            // After getting tag, then remove the tag
            if (end >= 0)
            {
                str = str.Substring(end + 1);
 
                // Additional starting tags to be romove
                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 current string instance matches the specified string when it compared using the specified culture. If match 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: 
 

CSHARP




// C# program to illustrate the
// String.StartsWith (string,
// bool, CultureInfo) Method
using System.Threading;
using System.Globalization;
using System;
 
class Sudo {
 
    // Main Method
    public static void Main(string[] args)
    {
 
        // Input string
        string str1 = "Geeks";
 
        // Implementation of startswith() function
 
        // test in original string.
        bool result_a = str1.StartsWith("Geeks", false,
                         CultureInfo.InvariantCulture);
 
        // test in small letter string.
        bool result_b = str1.StartsWith("geeks", false,
                         CultureInfo.InvariantCulture);
 
        // test in capital letter string.
        bool result_c = str1.StartsWith("GEEKS", false,
                         CultureInfo.InvariantCulture);
 
        // test in no string parameter .
        bool result_d = str1.StartsWith(" ", false,
                     CultureInfo.InvariantCulture);
 
        // Display result
        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 current string instance matches the specified string or not when compared using the specified comparison option. If match 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:
 

CSHARP




// C# program to illustrate the
// StartsWith(String, StringComparison)
// method
using System;
 
class Sudo {
     
    // Main Method
    public static void Main(string[] args)
    {
         
        // Input two string
        string str1 = "GeeksforGeeks";
        string str2 = "Learn CSharp";
 
        // Implementation of startswith() function
        // test for original string1 value.
        bool result_a = str1.StartsWith("Geek",
              StringComparison.CurrentCulture);
               
        // test for small letter string1 value .
        bool result_b = str1.StartsWith("geek",
               StringComparison.CurrentCulture);
 
        // test for string2 value .
        bool result_tt = str2.StartsWith("CSharp",
                  StringComparison.CurrentCulture);
                   
        bool result_t = str2.StartsWith("Learn",
               StringComparison.CurrentCulture);
 
        // Display result
        Console.WriteLine(result_a);
        Console.WriteLine(result_b);
        Console.WriteLine(result_tt);
        Console.WriteLine(result_t);
    }
}

Output: 
 

True
False
False
True

References: 
 

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :