javascript去除特定字符的方法是:1、使用replace函数替换,语法“元素.replace(‘需要去除的字符串’, ”)”;2、使用字符串分割函数再聚合。
本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
本文实例讲述了JS实现字符串中去除指定子字符串方法。分享给大家供大家参考,具体如下:
nbsp;html>Title /*方法一:使用replace函数替换*/ //去除字符串中含有的某字符串:str = str.replace('give', ''); var str = 'Could you please give me a simple example of how to'; console.log("str=======前==" + str);//str=======前==Could you please give me a simple example of how to //注意:此处不可写作:str.replace('give', '');要写作:str = str.replace('give', ''); // replace:返回新的字符串,一定要重新接收,不然替换不了 str = str.replace('give', '');//去掉字符的位置不定,可能在字符串中间,也可能在末尾 console.log("str.replace('give', '')==" + str.replace('give', '')); //str.replace('give', '')==Could you please me a simple example of how to console.log("str=======后==" + str);//str=======后==Could you please me a simple example of how to /*方法二:使用字符串分割函数再聚合*/ var str = "hello world!"; var items = str.split("o"); //会得到一个数组,数组中包括利用o分割后的多个字符串(不包括o) var newStr = items.join("");//数组转成字符串,元素是通过指定的分隔符进行分隔的。此时以空串分割:即直接连接 console.log("newStr=====" + newStr);// newStr=====hell wrld! //会得到一个新字符串,将数组中的数组使用空串连接成一个新字符串
登录后复制
运行结果:
立即学习“Java免费学习笔记(深入)”;
【推荐学习:javascript高级教程】
以上就是javascript如何去除特定字符的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2709861.html