字符串操作
slice substring substr substr split join String(1234) toString 都不会改变原字符
1.slice(start, end)
2.substring(start, end)
3.substr(start, number) // number是个数
4.split(’,’) // 根据, 拆分为数组
5.join(’’) // 数组转字符
6.String(1234)\
toString
7.concat
var stringValue = "hello ";
var result = stringValue.concat(“world”, “!”);
alert(result); //“hello world!”
alert(stringValue); //“hello”
8.item.toUpperCase() //转大写
item.toLowerCase() //转小写
9.charAt(index)
var str=“hello world”;
console.log(str.charAt(1));//e
console.log(str.charCodeAt(1))
str.charCodeAt(3) //index为3的Unicode 值,返回108
String.fromCharCode(108) // l
10.indexof
var str=“hello world”;
console.log(str.indexOf(“o”));//4
console.log(str.lastIndexOf(“o”));//7
console.log(str.indexOf(“o”,6));//7
console.log(str.lastIndexOf(“o”,6));//4’
str.includes(‘h’) // true
in
11.去除空格 str.trim()
var str=" hello world ";
console.log(’(’+str.trim()+’)’);//(hello world)
12.replace(原字符串一部分,要替换的部分)
var str="cat,bat,sat,fat";
var res=str.replace("at","one");//第一个参数是字符串,所以只会替换第一个子字符串
console.log(res);//cone,bat,sat,fat
var res1=str.replace(/at/g,"one");//第一个参数是正则表达式,所以会替换所有的子字符串
console.log(res1);//cone,bone,sone,fone
13.search
var str = “ABCDECDF”;
str.search(“CD”); // 或 str.search(/CD/i);
结果:2

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



