怎样使用JS做出字符串内去除指定子字符串

如题所述

两种方式可以实现

1:使用replace函数替换
var str="hello world!";
str=str.replace("l","");

即使用空串替换某一个字符串,则是可以实现去除指定字符串功能

2:使用字符串分割函数在聚合
var str="hello world!"

var items=str.split("o")
会得到一个数组,数组中包括利用o分割后的多个字符串(不包括o)

var newStr=items.join("");
会得到一个新字符串,将数组中的数组使用空串连接成一个新字符串
温馨提示:答案为网友推荐,仅供参考
相似回答