endsWith in JavaScript

前端 未结 30 1676
-上瘾入骨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:09

    @chakrit's accepted answer is a solid way to do it yourself. If, however, you're looking for a packaged solution, I recommend taking a look at underscore.string, as @mlunoe pointed out. Using underscore.string, the code would be:

    function endsWithHash(str) {
      return _.str.endsWith(str, '#');
    }
    

提交回复
热议问题