Jef*_*ins 0 javascript arrays substr
我正在尝试编写javascript代码,测试第一个字符串的结尾是否与目标相同,返回true.否则,返回false.必须使用.substr()来获得结果.
function end(str, target) {
myArray = str.split();
//Test if end of string and the variables are the same
if (myArray.subsrt(-1) == target) {
return true;
}
else {
return false;
}
}
end('Bastian', 'n');
Run Code Online (Sandbox Code Playgroud)
尝试:
function end(str, target) {
return str.substring(str.length-target.length) == target;
}
Run Code Online (Sandbox Code Playgroud)
更新:
在新的浏览器中,您可以使用:string.prototype.endsWith,但IE需要使用polyfill(您可以使用包含polyfill的https://polyfill.io,不要为现代浏览器返回任何内容,它对其他浏览器也很有用)与IE相关的事情).
| 归档时间: |
|
| 查看次数: |
2100 次 |
| 最近记录: |