How to know that a string starts/ends with a specific string in jQuery?
JavascriptJqueryStringJavascript Problem Overview
I want to know if a string starts with the specified character/string or ends with it in jQuery.
For Example:
var str = 'Hello World';
if( str starts with 'Hello' ) {
alert('true');
} else {
alert('false');
}
if( str ends with 'World' ) {
alert('true');
} else {
alert('false');
}
If there is not any function then any alternative ?
Javascript Solutions
Solution 1 - Javascript
One option is to use regular expressions:
if (str.match("^Hello")) {
// do this if begins with Hello
}
if (str.match("World$")) {
// do this if ends in world
}
Solution 2 - Javascript
For startswith, you can use indexOf:
if(str.indexOf('Hello') == 0) {
...
and you can do the maths based on string length to determine 'endswith'.
if(str.lastIndexOf('Hello') == str.length - 'Hello'.length) {
Solution 3 - Javascript
There is no need of jQuery to do that. You could code a jQuery wrapper but it would be useless so you should better use
var str = "Hello World";
window.alert("Starts with Hello ? " + /^Hello/i.test(str));
window.alert("Ends with Hello ? " + /Hello$/i.test(str));
as the match() method is deprecated.
PS : the "i" flag in RegExp is optional and stands for case insensitive (so it will also return true for "hello", "hEllo", etc.).
Solution 4 - Javascript
You do not really need jQuery for such tasks. In the ES6 specification they already have out of the box methods startsWith and endsWith.
var str = "To be, or not to be, that is the question.";
alert(str.startsWith("To be")); // true
alert(str.startsWith("not to be")); // false
alert(str.startsWith("not to be", 10)); // true
var str = "To be, or not to be, that is the question.";
alert( str.endsWith("question.") ); // true
alert( str.endsWith("to be") ); // false
alert( str.endsWith("to be", 19) ); // true
Currently available in FF and Chrome. For old browsers you can use their polyfills or substr
Solution 5 - Javascript
You can always extend String
prototype like this:
// Checks that string starts with the specific string
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (str) {
return this.slice(0, str.length) == str;
};
}
// Checks that string ends with the specific string...
if (typeof String.prototype.endsWith != 'function') {
String.prototype.endsWith = function (str) {
return this.slice(-str.length) == str;
};
}
And use it like this:
var str = 'Hello World';
if( str.startsWith('Hello') ) {
// your string starts with 'Hello'
}
if( str.endsWith('World') ) {
// your string ends with 'World'
}
Solution 6 - Javascript
ES6 now supports the startsWith()
and endsWith()
method for checking beginning and ending of string
s. If you want to support pre-es6 engines, you might want to consider adding one of the suggested methods to the String
prototype.
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (str) {
return this.match(new RegExp("^" + str));
};
}
if (typeof String.prototype.endsWith != 'function') {
String.prototype.endsWith = function (str) {
return this.match(new RegExp(str + "$"));
};
}
var str = "foobar is not barfoo";
console.log(str.startsWith("foob"); // true
console.log(str.endsWith("rfoo"); // true