endsWith in JavaScript

前端 未结 30 1408
-上瘾入骨i
-上瘾入骨i 2020-11-22 05:41

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

Example: I have a string

var str = \"mystring#\";

I wa

30条回答
  •  渐次进展
    2020-11-22 06:04

    Its been many years for this question. Let me add an important update for the users who wants to use the most voted chakrit's answer.

    'endsWith' functions is already added to JavaScript as part of ECMAScript 6 (experimental technology)

    Refer it here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

    Hence it is highly recommended to add check for the existence of native implementation as mentioned in the answer.

提交回复
热议问题