字符串操作常用的方法

在javascript当中,主要操作的对象可以简单的理解为是字符串,数组和键值对,因此字符串常用的操作方法需熟悉掌握。
charCodeAt 方法返回一个整数,代表指定位置字符的Unicode编码。Unicode 编码单元(code points)的范围从 0 到 1,114,111(0x10FFFF)。注意:charCodeAt 总是返回一个小于 65,536 的值。
charAt 方法返回指定索引位置处的字符。如果超出有效范围的引值返回空字符串,如果没有提供参数,则默认的是0。
slice 方法返回字符串的片段。slice() 提取的新字符串包括beginSlice但不包括 endSlice。
substring 方法返回位于String 对象中指定位置的子字符串。
substr 方法返回一个从指定位置开始的指定长度的子字符串。

indexOf 方法返回String 对象内第一次出现子字符串位置。如果没有找到子字符串,则返回-1。
lastIndexOf 方法返回String 对象中字符串最后出现的位置。如果没有匹配到子字符串,则返回-1。
search 方法返回与正则表达式查找内容匹配的第一个字符串的位置。
concat concat() 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。
split 将一个字符串分割为子字符串,然后将结果作为字符串数组返回。
match(searchvalue||regexp) 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。括号内的条件必须填写。
replace(regexp,replacement) 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
toLowerCase() 方法用于把字符串转换为小写。一个新的字符串,在其中 stringObject 的所有大写字符全部被转换为了小写字符。
toUpperCase() 方法用于把字符串转换为大写。一个新的字符串,在其中 stringObject 的所有小写字符全部被转换为了大写字符。
trim() 方法会从一个字符串的两端删除空白字符。

通过简单的demo来具体说明以上的方法的使用情况。

1
2
3
charCodeAt() 语法:str.charCodeAt(index)
"ABC".charCodeAt(0) //return 65
注意:如果指定的 index 小于 0 或不小于字符串的长度,则 charCodeAt 返回 NaN
1
2
3
4
charAt() 语法:str.charAt(index)
"hello a".charAt(0) //return h
"hello a".charAt(5) //return ' ' ,空格也占位置。
"hello a".charAt(8) //return '',当索引值大于length-1后,将会返回空字符串。
1
2
3
4
slice() 语法:str.slice(beginSlice,endSlice)
"hello my wold".slice(0,11) //return hello my wo
"hello my wold".slice(0,-2) //return hello my wo,如果endSlice该参数为负数,则被看作是 sourceLength + endSlice
注意:slice() 提取的新字符串包括beginSlice但不包括 endSlice。
1
2
3
4
5
6
7
8
9
10
11
substring() 语法:str.substring(indexStart, indexEnd)
"forever I will rember!".substring(0,5) //return forev
"forever I will rember!".substring(5,1) //return orev
"forever I will rember!".substring(-2,5) //return forev
"forever I will rember!".substring(NaN,5) //return forev
注意: 1. substring 提取从 indexStart 到 indexEnd(不包括)之间的字符。
2. 如果 indexStart 等于 indexEnd,substring 返回一个空字符串。
3. 如果省略 indexEnd,substring 提取字符一直到字符串末尾。
4. 如果任一参数小于 0 或为 NaN,则被当作 0
5. 如果任一参数大于 stringName.length,则被当作 stringName.length。
6. 如果 indexStart 大于 indexEnd,则 substring 的执行效果就像两个参数调换了一样。
1
2
3
4
5
6
7
substr() 语法:substr(start,length)
"abcdefghi".substr(1,5) //return bcdef
"abcdefghi".substr(-20,2) //return ab
"abcdefghi".substr(0) //return abcdefghi
注意:1. 开始提取字符的位置。如果为负值,则被看作 strLength + start。
2. 如果只传一个参数,则表示从开始的索引开始,一直到结束。
3. 如果 start 为正值,且大于或等于字符串的长度,则 substr 返回一个空字符串。
1
2
3
4
indexOf() 语法:indexOf(searchValue,fromIndex)
"hello my friend".indexOf("friend",3) //return 9
"hello my friend".indexOf("frient",3) //return -1
注意:指定值的第一次出现的索引; 如果没有找到 -1
1
2
3
lastIndexOf() 语法:lastIndexOf(searchValue,fromIndex)
"hello my friend".lastIndexOf("friend") //return 9
注意:指定值的第一次出现的索引; 如果没有找到 -1
1
2
3
4
search() 语法:str.search(regexp) 如果匹配成功,则 search() 返回正则表达式在字符串中首次匹配项的索引。否则,返回 -1
"hello my friend".search(/\s/) //return 5
"hello my friend".search(/\d/) //return -1
注意:指定值的第一次出现的索引; 如果没有找到 -1
1
2
3
4
concat() 语法:str.concat(other str)
"hello my friend".concat(",forever friend") //return hello my friend,forever friend
"hello my friend" + ",forever friend" //return hello my friend,forever friend
注意:如果是字符串的拼接,直接用+号或者是+=号连接会更好。
1
2
3
split() 语法:str.split([separator[, limit]])
"hello my friend".split(" ") //return ["hello", "my", "friend"]
注意:当字符串为空时,split 返回一个包含一个空字符串的数组,而不是一个空数组。它一般和数组的join()方法结合使用。
1
2
3
match() 语法:str.match(regexp)
"hello my friend".match(/my/) //return ["my", index: 6, input: "hello my friend"]
注意:如果正则表达式包含 g 标志,则该方法返回一个 Array ,它包含所有匹配的子字符串而不是匹配对象。捕获组不会被返回(即不返回index属性和input属性)。如果没有匹配到,则返回 null
1
2
toLowerCase() 语法:str.toLowerCase()
"HELLO MY FRIEND".toLowerCase() //return hello my friend
1
2
toLowerCase() 语法:str.toUpperCase()
"hello my friend".toUpperCase() //return HELLO MY FRIEND
1
2
3
4
5
6
7
8
trim() 语法:str.trim()
"hello ".toUpperCase() //return hello
if (!String.prototype.trim) { //兼容旧版本
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
注意:trim() 方法并不影响原字符串本身,它返回的是一个新的字符串。