Javascript: How to check if a string starts with another string (4 ways)

While working in javascript, we often come across a general requirement to check if a string “StartsWith” another string. This article will check if a javascript string will start with a specific substring using different methods and example illustrations.

Table of Contents:-

Check if a starting starts with another string using startsWith() function

The javascript function startsWith() finds out whether a string begins with particular characters of a specified string. 

Function Code:-

function stringStartsWithString(_superstring,_substring)
{
if(_superstring.startsWith(_substring))
{
    console.log("The string starts with the given substring")
}
else{
    console.log("The string does not start with the given substring")
}
}

Usage:-

let superString = "Javascript is the most popular language"
let subString1 = "Hello"
let subString2 = "Java"
stringStartsWithString(superString, subString1)
stringStartsWithString(superString, subString2)

Output:-

The string does not start with the given substring
The string starts with the given substring

Check if a starting starts with another string using lastIndex() function

Function lastIndexOf() in javascript is used to return the index of the last occurrence of specified value within the calling string object. This function does a backward search.

Function Code:-

function stringStartsWithString(_superstring,_substring)
{
if(_superstring.lastIndexOf(_substring, 0) === 0)
{
    console.log("The string starts with the given substring")
}
else{
    console.log("The string does not start with the given substring")
}
}

Usage:-

let superString = "Javascript is the most popular language"
let subString1 = "Java"
let subString2 = "Java is"
stringStartsWithString(superString, subString1)
stringStartsWithString(superString, subString2)

Output:-

The string starts with the given substring
The string does not start with the given substring

Check if a string starts with another string using Regex and test() function

Here test() function is used, which executes the search to find out if the string and the regular expression are matching or not.

Function Code:-

function stringStartsWithString(_superstring,_substring)
{
if(new RegExp('^' + _substring).test(_superstring))
{
    console.log("The string starts with the given substring")
}
else{
    console.log("The string does not start with the given substring")
}
}

Usage:-

let superString = "Javascript is the most popular language"
let subString1 = "Java"
let subString2 = "Java is"
stringStartsWithString(superString, subString1)
stringStartsWithString(superString, subString2)

Output:-

The string starts with the given substring
The string does not start with the given substring

Check if a starting starts with another string using custom function

Function Code:-

function stringStartsWithString(_superstring,_substring) 
 {
   for (let i = 0, pos; i < _substring.length; i++) {
      pos = _substring[i];
      if (_superstring[i] !== _substring[i])
       {
        return false;
       }
    }
    return true;
  }
 if(stringStartsWithString("Javascript is the most popular language", "Java"))
 {
     console.log("The string starts with the given substring")
 }
 else
 {
    console.log("The string does not start with the given substring")
 }

Usage1:-

 let superString = "Javascript is the most popular language"
 let subString = "Java"
 if(stringStartsWithString(superString, subString))
 {
     console.log("The string starts with the given substring")
 }
 else
 {
    console.log("The string does not start with the given substring")
 }

Output:-

The string starts with the given substring

Usage2:-

 let superString = "Javascript is the most popular language"
 let subString = "Java is"
 if(stringStartsWithString(superString, subString))
 {
     console.log("The string starts with the given substring")
 }
 else
 {
    console.log("The string does not start with the given substring")
 }

Output:-

The string does not start with the given substring

Read More:

We hope this article helped you check if a javascript string starts with another string. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top