let str = 'example@';
if (str.endsWith('@')) {
str = str.slice(0, -1); // 删除最后一个字符
}
console.log(str); // 输出: 'example'
JS判断最后一个字符是不是@,如果是则删除
本文介绍了如何在JavaScript中使用`endsWith`和`slice`方法,展示如何从字符串变量`str`中移除以@结尾的字符,最终输出示例字符串。
本文介绍了如何在JavaScript中使用`endsWith`和`slice`方法,展示如何从字符串变量`str`中移除以@结尾的字符,最终输出示例字符串。
let str = 'example@';
if (str.endsWith('@')) {
str = str.slice(0, -1); // 删除最后一个字符
}
console.log(str); // 输出: 'example'

被折叠的 条评论
为什么被折叠?