String 对象方法

var a="abcdc";

var b="a+d+c";

对象

作用

例子

charAt(num)

返回在指定位置的字符

alert(a.charAt(3));

//d

charCodeAt(num)

返回指定位置的字符的Unicode编码

alert(a.charCodeAt(3));

//100

fromCharCode()

接受一个或多个指定的Unicode值,然后返回一个或多个字符串。

alert(String.fromCharCode(76,77,78));

//LMN

concat()

连接字符串。

indexOf()

返回某个指定的字符串,在字符串中首次出现的位置

alert(a.indexOf("c"));

//2

lastIndexOf()

方法可返回一个指定的字符串值最后出现的位置,区分大小写。

alert(a.lastIndexOf("c"));

//4

match()

在字符串中检索指定的值,而非位置。

var str="Hello world!"

alert(str.match("world"))

//world

replace()

替换与正则表达式匹配的子串。

alert(a.replace("ab","aa"));

alert(a);

//aacd

//abcd

search()

检索与正则表达式相匹配的值。

返回位置

alert( a.search(/c/));

//2

slice(start,end)

从指定的开始位置,到结束位置(不包括)的所有字符串。如果不指定结束位置,则从 指定的开始位置,取到结尾

alert(a.slice(1,3));

//bc

split("分割位置",[指定的长度])

将一个字符串分割成数组

alert( a.split() instanceof Array);

//true

alert( b.split("+"));

//a,b,c

substr(start,length)

截取指定长度的字符串

alert(a.substr(1,2));

//bc

substring(start,end)

从指定的开始位置,到结束位置(不包括)的所有字符串。如果不指定结束位置,则从 指定的开始位置,取到结尾

alert(a.substring(1,3));

//bc

猜你喜欢

转载自blog.csdn.net/xiaohanzhu000/article/details/80891210
今日推荐