The startsWith() method returns true if a string starts with a specified string. Otherwise it returns false . The startsWith() method is case sensitive.
The startsWith() method checks if the string starts with the particular string. The endsWith() method checks if the string ends with the particular string.
In this case, we will use the includes() method which determines whether a string contains the specified word or a substring. If the word or substring is present in the given string, the includes() method returns true; otherwise, it returns false. The includes() method is case sensitive.
To check if a string starts with a substring, call the indexOf() method on the string, passing it the substring as a parameter. If the indexOf method returns 0 , then the string starts with the substring, otherwise it doesn't.
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
}
For startswith, you can use indexOf:
if(str.indexOf('Hello') == 0) {
...
ref
and you can do the maths based on string length to determine 'endswith'.
if(str.lastIndexOf('Hello') == str.length - 'Hello'.length) {
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.).
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
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'
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With