Open In App

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

Last Updated : 20 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will check a string begins with some given characters/pattern. We can check whether a given string starts with the characters of the specified string or not by various methods in javascript which are described below.

Methods to check if a string begins with some given pattern:

Method 1: Using JavaScript loops

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

Syntax:

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

Example: Below program demonstrates the above approach: 

javascript




// 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
  let result = true
 
  // Loop to match character by character
  for (let 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) {
    console.log('String begins with "' + pattern + '"')
  } else {
    console.log("String doesn't " + 'begins with "' + pattern + '"')
  }
}
 
// Driver code
// String to check
let str = 'GeeksforGeeks'
 
// Pattern by which string
// begins or not
let pattern = 'Geeks'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Call check function
check(str, pattern)
 
// Change string
str = 'geeksforgeeks'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Calling check function
check(str, pattern)


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: Using substring() and localeCompare() Methods

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

Syntax:

let substr = string.substring(Startindex, Endindex)
string.localeCompare(substr)

Example: Below program demonstrates the above approach: 

javascript




// 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
  let sub_str = str.substring(0, pattern.length)
 
  if (sub_str.localeCompare(pattern) == 0) {
    console.log('String begins with "' + pattern + '"')
  } else {
    console.log("String doesn't " + 'begins with "' + pattern + '"')
  }
}
 
// Driver code
// String to check
let str = 'GeeksforGeeks'
 
// Pattern by which string
// begins or not
let pattern = 'Geeks'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Call check function
check(str, pattern)
 
// Change string
str = 'geeksforgeeks'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Calling check function
check(str, pattern)


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: Using string startsWith() method

This is the best solution above all, in this method, we will use the 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: 

javascript




// 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)) {
    console.log('String begins with "' + pattern + '"')
  } else {
    console.log("String doesn't " + 'begins with "' + pattern + '"')
  }
}
 
// Driver code
// String to check
let str = 'Burn to shine'
 
// Pattern by which string
// begins or not
let pattern = 'Burn'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Call check function
check(str, pattern)
 
// Change string
str = 'Happy coding'
// Change pattern
pattern = 'happy'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Calling check function
check(str, pattern)


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"

Method 4: Using JavaScript indexOf() method

Example: In this example, we will use JavaScript string indexOf method to check if the pattern is present at index zero it returns true else it returns false

Javascript




// 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.indexOf(pattern) === 0) {
    console.log('String begins with "' + pattern + '"')
  } else {
    console.log("String doesn't " + 'begins with "' + pattern + '"')
  }
}
 
// Driver code
// String to check
let str = 'Burn to shine'
 
// Pattern by which string
// begins or not
let pattern = 'Burn'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Call check function
check(str, pattern)
 
// Change string
str = 'Happy coding'
// Change pattern
pattern = 'happy'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Calling check function
check(str, pattern)


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"



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads