问题:
How can I check if a string ends with a particular character in JavaScript? 如何在JavaScript中检查字符串是否以特定字符结尾?
Example: I have a string 示例:我有一个字符串
var str = "mystring#";
I want to know if that string is ending with #
. 我想知道该字符串是否以#
结尾。 How can I check it? 我该如何检查?
Is there a
endsWith()
method in JavaScript? JavaScript中是否有endsWith()
方法?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? 这是最好的方法还是还有其他方法?
解决方案:
参考一: https://stackoom.com/question/1B0M/JavaScript中的endsWith
参考二: https://oldbug.net/q/1B0M/endsWith-in-JavaScript