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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>                    

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>                    

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>                    

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.