假如我要选取world 我知道的是用var str="hello world!";
console.log(str.substring(6,11));str.split(" "); //这样分割取值也行有什么其他不改变原来的字符串的方法吗?
1 回答

梦里花落0921
TA贡献1772条经验 获得超6个赞
console.log('substr:', str.substr(6));
console.log('substring:', str.substring(6));
console.log('slice:', str.slice(6));
console.log('split:', str.split(' ')[1]);
console.log('原字符串:', str);
字符串是基本数据类型,当你确定后,是不会改变的。
当你使用上述的方法,是后台自动调用了new String(str)生成一个对象,在这个对象上调用相应的方法,调用完毕后,会自动删除这个对象,所以原字符串根本没有改变。
建议你看一下基本包装类型,就会明白的,加油!
添加回答
举报
0/150
提交
取消