Check String Using Built-in Methods
JavaScript
xxxxxxxxxx
function checkString(str) {
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');
}
}
let string = prompt('Enter a string: ');
checkString(string);
Enter a string: String The string starts with S but does not end with G
Check The String Using Regex
JavaScript
xxxxxxxxxx
function checkString(str) {
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 (let i = 0; i < 3; i++) {
const string = prompt('Enter a string: ');
checkString(string);
}
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
Linux - How to Install anc-api-tools
Linux - How to Install curvedns
Python - Check if a Number is Positive, Negative or 0
Linux - How to Install zenmap
C - Count Number of Digits in an Integer
C++ - Check Whether a Number is Prime or Not
C++ - Check Leap Year
C - Find Largest Number Using Dynamic Memory Allocation
Kotlin - Reverse a Number
Linux - How to Install zfs-test
C++ - Find Quotient and Remainder
Kotlin - Round a Number to n Decimal Places
Linux - How to Install xbitmaps
Kotlin - Find Factorial of a Number
Java - Reverse a Number
Linux - How to Install zstd
GoLang - Base64 Encoding
GoLang - Range
We have been online since 2021 and 1 millions of people around the globe have visited our website since then
More visitors every month