Algorithm
-
Input:
- Accept the input string.
- Accept the desired starting and ending characters.
-
Validation:
- Ensure that the input string and the specified starting and ending characters are not empty.
-
Check Starting Characters:
- Extract the substring from the beginning of the input string with a length equal to the length of the starting characters.
- Compare this substring with the specified starting characters.
- If they match, continue to the next step; otherwise, the string does not start with the specified characters.
-
Check Ending Characters:
- Extract the substring from the end of the input string with a length equal to the length of the ending characters.
- Compare this substring with the specified ending characters.
- If they match, the string starts and ends with the specified characters; otherwise, it doesn't.
-
Output:
- Display a message indicating whether the string starts and ends with the specified characters.
Code Examples
#1 Code Example- Check String Using Built-in Methods
Code -
Javascript Programming
// program to check if a string starts with 'S' and ends with 'G'
function checkString(str) {
// check if the string starts with S and ends with G
if(str.startsWith('S') && str.endsWith('G')) {
console.log('The string starts with S and ends with G');
}
else if(str.startsWith('S')) {
console.log('The string starts with S but does not end with G');
}
else if(str.endsWith('G')) {
console.log('The string starts does not with S but end with G');
}
else {
console.log('The string does not start with S and does not end with G');
}
}
// take input
let string = prompt('Enter a string: ');
checkString(string);
Copy The Code &
Try With Live Editor
Output
Enter a string: String
The string starts with S but does not end with G
The string starts with S but does not end with G
#2 Code Example- Check The String Using Regex
Code -
Javascript Programming
// program to check if a string starts with 'S' and ends with 'G'
function checkString(str) {
// check if the string starts with S and ends with G
if( /^S/i.test(str) && /G$/i.test(str)) {
console.log('The string starts with S and ends with G');
}
else if(/^S/i.test(str)) {
console.log('The string starts with S but does not ends with G');
}
else if(/G$/i.test(str)) {
console.log('The string starts does not with S but ends with G');
}
else {
console.log('The string does not start with S and does not end with G');
}
}
// for loop to show different scenario
for (let i = 0; i < 3; i++) {
// take input
const string = prompt('Enter a string: ');
checkString(string>;
}
Copy The Code &
Try With Live Editor
Output
Enter a string: String
The string starts with S and ends with G
Enter a string: string
The string starts with S and ends with G
Enter a string: JavaScript
The string does not start with S and does not end with G
The string starts with S and ends with G
Enter a string: string
The string starts with S and ends with G
Enter a string: JavaScript
The string does not start with S and does not end with G
Demonstration
JavaScript Programing Example to Check Whether a String Starts and Ends With Certain Characters-DevsEnv