Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

endsWith in JavaScript

How can I check if a string ends with a particular character in JavaScript?

Example: I have a string

var str = "mystring#"; 

I want to know if that string is ending with #. How can I check it?

  1. Is there a endsWith() method in JavaScript?

  2. One solution I have is take the length of the string and get the last character and check it.

Is this the best way or there is any other way?

like image 375
Phani Kumar Bhamidipati Avatar asked Nov 11 '08 11:11

Phani Kumar Bhamidipati


People also ask

What is JavaScript endsWith?

The endsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.

How does endsWith () work?

The endswith() method returns a boolean. It returns True if a string ends with the specified suffix. It returns False if a string doesn't end with the specified suffix.

What does endsWith () do in Java?

The endsWith() method checks whether a string ends with the specified character(s). Tip: Use the startsWith() method to check whether a string starts with the specified character(s).

Can I use string endsWith?

Definition and UsageThe endsWith() method returns true if a string ends with a specified string. Otherwise it returns false . The endsWith() method is case sensitive. See also the startswith() method.


1 Answers

UPDATE (Nov 24th, 2015):

This answer is originally posted in the year 2010 (SIX years back.) so please take note of these insightful comments:

  • Shauna -

Update for Googlers - Looks like ECMA6 adds this function. The MDN article also shows a polyfill. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

  • T.J. Crowder -

Creating substrings isn't expensive on modern browsers; it may well have been in 2010 when this answer was posted. These days, the simple this.substr(-suffix.length) === suffix approach is fastest on Chrome, the same on IE11 as indexOf, and only 4% slower (fergetaboutit territory) on Firefox: jsperf.com/endswith-stackoverflow/14 And faster across the board when the result is false: jsperf.com/endswith-stackoverflow-when-false Of course, with ES6 adding endsWith, the point is moot. :-)


ORIGINAL ANSWER:

I know this is a year old question... but I need this too and I need it to work cross-browser so... combining everyone's answer and comments and simplifying it a bit:

String.prototype.endsWith = function(suffix) {     return this.indexOf(suffix, this.length - suffix.length) !== -1; }; 
  • Doesn't create a substring
  • Uses native indexOf function for fastest results
  • Skip unnecessary comparisons using the second parameter of indexOf to skip ahead
  • Works in Internet Explorer
  • NO Regex complications

Also, if you don't like stuffing things in native data structure's prototypes, here's a standalone version:

function endsWith(str, suffix) {     return str.indexOf(suffix, str.length - suffix.length) !== -1; } 

EDIT: As noted by @hamish in the comments, if you want to err on the safe side and check if an implementation has already been provided, you can just adds a typeof check like so:

if (typeof String.prototype.endsWith !== 'function') {     String.prototype.endsWith = function(suffix) {         return this.indexOf(suffix, this.length - suffix.length) !== -1;     }; } 
like image 199
chakrit Avatar answered Sep 29 '22 10:09

chakrit