JavaScript or jQuery string ends with utility function

JavascriptJquery

Javascript Problem Overview


what is the easiest way to figure out if a string ends with a certain value?

Javascript Solutions


Solution 1 - Javascript

you could use Regexps, like this:

str.match(/value$/)

which would return true if the string has 'value' at the end of it ($).

Solution 2 - Javascript

Stolen from prototypejs:

String.prototype.endsWith = function(pattern) {
    var d = this.length - pattern.length;
    return d >= 0 && this.lastIndexOf(pattern) === d;
};

'slaughter'.endsWith('laughter');
// -> true

Solution 3 - Javascript

Regular expressions

"Hello world".match(/world$/)

Solution 4 - Javascript

I had no luck with the match approach, but this worked:

If you have the string, "This is my string." and wanted to see if it ends with a period, do this:

var myString = "This is my string.";
var stringCheck = ".";
var foundIt = (myString.lastIndexOf(stringCheck) === myString.length - stringCheck.length) > 0;
alert(foundIt);

You can change the variable stringCheck to be any string to check for. Better still would be to throw this in your own function like this:

function DoesStringEndWith(myString, stringCheck)
{
    var foundIt = (myString.lastIndexOf(stringCheck) === myString.length - stringCheck.length) > 0;
    return foundIt;
}

Solution 5 - Javascript

You can do 'hello world'.slice(-5)==='world'. Works in all browsers. Much faster than regex.

Solution 6 - Javascript

ES6 supports this directly:

'this is dog'.endsWith('dog')  //true

Solution 7 - Javascript

I am just expanding on what @luca-matteis has posted but to solve the issues pointed out in the comments the code should be wrapped to make sure you are not overwriting a native implementation.

if ( !String.prototype.endsWith ) {  
    String.prototype.endsWith = function(pattern) {
        var d = this.length - pattern.length;
        return d >= 0 && this.lastIndexOf(pattern) === d;
    };
}

This is the suggested method for the Array.prototype.forEach method pointed out in the mozilla developer network

Solution 8 - Javascript

You can always prototype String class, this will work:

String.prototype.endsWith = function(str) {return (this.match(str+"$")==str)}

You can find other related extensions for String class in http://www.tek-tips.com/faqs.cfm?fid=6620

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJon EricksonView Question on Stackoverflow
Solution 1 - JavascriptcloudheadView Answer on Stackoverflow
Solution 2 - JavascriptLuca MatteisView Answer on Stackoverflow
Solution 3 - JavascriptChetan SView Answer on Stackoverflow
Solution 4 - JavascripttheJermView Answer on Stackoverflow
Solution 5 - JavascriptJohn HenckelView Answer on Stackoverflow
Solution 6 - JavascriptMr. GoferitoView Answer on Stackoverflow
Solution 7 - JavascriptgeorgephillipsView Answer on Stackoverflow
Solution 8 - JavascriptJaime HablutzelView Answer on Stackoverflow