Skip to content
Related Articles

Related Articles

Improve Article

How to check a string begins with some given characters/pattern ?

  • Last Updated : 10 Jun, 2019

We can check that given string starts with the characters of the specified string or not by various methods in javascript which are described below:

Method 1: This is a simple approach in which we will match character one by one from starting using the loop and if any character doesn’t matches then we can say that string doesn’t begin with the character or specified string.

Syntax:

for (var i = 0; i < pattern.length; i++) {
        if(str.charAt(i) != pattern.charAt(i)){
            result = false;
            break;
        }
      }

Example: Below program demonstrates the above approach:




<script>
    // Javascript script to check 
    // whether the String begins
    // with something or not
      
    // Function to check String
    // begins with something or not
    function check(str, pattern){
      // Initially we assume that String
      // begins with something
      // so result is true
      var result = true;
        
      // Loop to match character by character
      for (var i = 0; i < pattern.length; i++) {
          
        // If any character doesn't matches 
        // then result is false
        if(str.charAt(i) != pattern.charAt(i)){
            result = false;
            break;
        }
      }
        
      if(result){
        document.write("String begins with \""
                + pattern + "\"<br><br>");
        }
      else{
        document.write("String  doesn't "
            "begins with \"" + pattern + "\"<br><br>");
        }
    }
  
    // Driver code
    // String to check
    var str = "GeeksforGeeks";
      
    // Pattern by which string 
    // begins or not
    var pattern = "Geeks";
      
    document.write("String = \"" + str + "\"<br>");
    document.write("String should begin with  = \""
            + pattern + "\"<br>");
      
    // Call check function
    check(str, pattern);
      
    // Change string
    str = "geeksforgeeks";
      
   document.write("String = \"" + str + "\"<br>");
    document.write("String should begin with  = \""
            + pattern + "\"<br>");
              
    // Calling check function
    check(str, pattern);
</script>                    

Output:



String = "GeeksforGeeks"
String should begin with = "Geeks"
String begins with "Geeks"

String = "geeksforgeeks"
String should begin with = "Geeks"
String doesn't begins with "Geeks"

Method 2: In this method we will use substring() function to get substring of required length of pattern string and then match substring with pattern using localeCompare() function.

Syntax:

var = string.substring(Startindex, Endindex)
var.localeCompare(compareString)

Example: Below program demonstrates the above approach:




<script>
    // Javascript script to check 
    // whether the String begins
    // with something or not
      
    // Function to check String
    // begins with something or not
    function check(str, pattern){
      // Extracting substring of
      // pattern length
      var sub_str = 
            str.substring(0, pattern.length);
        
      if(sub_str.localeCompare(pattern) == 0){
        document.write("String begins with \""
                + pattern + "\"<br><br>");
        }
      else{
        document.write("String  doesn't "
            "begins with \"" + pattern + "\"<br><br>");
        }
    }
  
    // Driver code
    // String to check
    var str = "GeeksforGeeks";
      
    // Pattern by which string 
    // begins or not
    var pattern = "Geeks";
      
    document.write("String = \"" + str + "\"<br>");
    document.write("String should begin with  = \""
            + pattern + "\"<br>");
      
    // Call check function
    check(str, pattern);
  
    // Change string
    str = "geeksforgeeks";
      
   document.write("String = \"" + str + "\"<br>");
    document.write("String should begin with  = \""
            + pattern + "\"<br>");
              
    // Calling check function
    check(str, pattern);
</script>                    

Output:

String = "GeeksforGeeks"
String should begin with = "Geeks"
String begins with "Geeks"

String = "geeksforgeeks"
String should begin with = "Geeks"
String doesn't begins with "Geeks"

Method 3: This is the best solution above all, in this method we will use startsWith() method to directly check whether the given string starts with something or not.

Syntax:

str.startsWith( searchString , position )

Example: Below program demonstrates the above approach:




<script>
    // Javascript script to check 
    // whether the String begins
    // with something or not
      
    // Function to check String
    // begins with something or not
    function check(str, pattern){
        
      if(str.startsWith(pattern)){
        document.write("String begins with \""
                + pattern + "\"<br><br>");
        }
      else{
        document.write("String  doesn't "
            "begins with \"" + pattern + "\"<br><br>");
        }
    }
  
    // Driver code
    // String to check
    var str = "Burn to shine";
      
    // Pattern by which string 
    // begins or not
    var pattern = "Burn";
      
    document.write("String = \"" + str + "\"<br>");
    document.write("String should begin with  = \""
            + pattern + "\"<br>");
      
    // Call check function
    check(str, pattern);
      
    // Change string
    str = "Happy coding";
    // Change pattern
    pattern = "happy";
      
   document.write("String = \"" + str + "\"<br>");
    document.write("String should begin with  = \""
            + pattern + "\"<br>");
              
    // Calling check function
    check(str, pattern);
</script>                    

Output:

String = "Burn to shine"
String should begin with = "Burn"
String begins with "Burn"

String = "Happy coding"
String should begin with = "happy"
String doesn't begins with "happy"



My Personal Notes arrow_drop_up
Recommended Articles
Page :