字符串的方法详解
1.split(): 将字符串转化为数组,join将数组转化为字符串。不会改变原字符串
var a = "a,b,c,s"; var b = a.split(","); console.log(a);//a,b,c,s console.log(b);//["a", "b", "c", "s"]
2.substr(start, length):参数一表示开始的位置,参数二表示提取字符的长度。不会改变原字符串。但start为负数时,length无效。-1为倒数第一个数字,以此类推。
var a = "abcdefg"; var b = a.substr(2,3); console.log(a);//abcdefg console.log(b);//cde
3.charAt(): 返回该位置的字符
var a = "abcdefg"; var b = a.charAt(2); console.log(a);//abcdefg console.log(b);//c
4.charCodeAt(): 返回位置的字符编码,用于判断字符,例获取字节。
var a = "abcdefg"; var b = a.charCodeAt(0); console.log(a);//abcdefg console.log(b);//97
5.concat: 连接字符
var a = "a"; var b = a.concat("b", "c"); console.log(a);//a console.log(b);//abc
6.substring(start, end): 获取字符,不接受负数。
var a = "abcdefg"; var b = a.substring(2, 3); console.log(a);//abcdefg console.log(b);//c
7.indexOf: 返回第一个字符的位置
var text = "Mississippi"; var a = text.indexOf("ss"); var b = text.indexOf("ss", 3); var c = text.indexOf("ss", 6); console.log(a);//2 console.log(b);//5 console.log(c);//-1
8.lastIndexOf: 从末尾开始查找
var text = "Mississippi"; var a = text.lastIndexOf("ss"); var b = text.lastIndexOf("ss", 5); var c = text.lastIndexOf("ss", 6); console.log(a);//5 console.log(b);//2,在[0, 3]范围内找 console.log(c);//5,在[0, 6]范围内找
9.toLowerCase(): 变为小写
var a = "aBCDefg"; var b = a.toLowerCase(); console.log(a);//aBCDefg console.log(b);//abcdefg
10.toUpperCase(): 变为大写
var a = "aBCDefg"; var b = a.toUpperCase(); console.log(a);//aBCDefg console.log(b);//ABCDEFG
11.search()、match()、replace()用法见文章正则表达式
12.slice()用法见文章数组详解