How to check a string begins with some given characters/pattern ?
Last Updated :
20 Jul, 2023
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
function check (str, pattern) {
let result = true
for (let i = 0; i < pattern.length; i++) {
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 + '"' )
}
}
let str = 'GeeksforGeeks'
let pattern = 'Geeks'
console.log( 'String = "' + str + '"' )
console.log( 'String should begin with = "' + pattern + '"' )
check(str, pattern)
str = 'geeksforgeeks'
console.log( 'String = "' + str + '"' )
console.log( 'String should begin with = "' + pattern + '"' )
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"
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
function check (str, pattern) {
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 + '" ')
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"
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
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 + '" ')
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"
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
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 + '" ')
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"
Share your thoughts in the comments
Please Login to comment...