且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用JavaScript删除字符串中的最后一个单词

更新时间:2023-02-22 12:12:04

var str = "I want to remove the last word.";
var lastIndex = str.lastIndexOf(" ");

str = str.substring(0, lastIndex);

获取最后一个空格,然后获取子字符串。

Get last space and then get sub string.