String类型

1.几乎每个值都有toString()方法,NULL和undefined没有。

toString()方法以十进制格式返回数值的字符串表示。而通过传递基数,toString()可以输出以二进制、八进制、十六进制,乃至其他任意有效进制格式表示的字符串值。

num.toString(2)二进制    num.toString(8)八进制    num.toString(10)十进制     num.toString(16)十六进制

在不知道要转换的值是不是null和undefined的情况下,还可以使用转型函数String(),这个函数可以将任何类型的值转换为字符串:

1)如果有toString()方法,则调用该方法并返回相应结果

2)如果是null,返回null

3)如果是undefined,返回undefined;

 

2.字符方法

charAt()和charCodeAt()

var str='hello world';

alert(str.charAt(1))//e

alert(str.charCodeAt(1))//101

还可以使用方括号加索引来访问字符串中特定字符。eg:  str[1]

 

3.字符串操作方法

1)concat()用于将一个或多个字符串拼接起来,返回拼接得到的新字符串。可以接收任意多个参数,也就是说可以通过它拼接任意多个字符串。

var str='hello ';

var result=str.concat('world','!');

alert(str)// hello

alert(result)  //hello world!

 

2)slice() substr() substring()和concat()方法一样,不会修改字符串本身的值。

这三个方法基于子字符串创建新字符串,返回被操作字符串的一个子字符串,而且也都接受一或两个参数。第一个参数指定子字符串开始的位置。

slice() substring()的第二个参数指定的是子字符串最后一个字符后面的位置。

substr()的第二个参数指定的则是返回的字符的个数。

当传递给这些方法的参数是负值的情况下,slice()将传入的负值与字符串长度相加。substr()将负的第一个参数加上字符串长度,负的第二个参数转换为0; substring()把所有负值参数都转换为0,这个方法会将较小的数作为开始位置,将较大的数作为结束位置。

var str='hello world';  //length=11

alert(str.slice(-3))// str.slice(8)  rld

alert(str.substring(-3)) //str.substring(0)  hello world

alert(str.substr(-3)) //str.substr(8)  rld

alert(str.slice(3,-4))// str.slice(3,7)   lo w

alert(str.substring(3,-4))  //str.substring(3,0)即str.substring(0,3)  hel

alert(str.substr(3,-4))//str.substr(3,0) ''空字符串

 

3)indexOf() lastIndexOf()

var str='Lorem ipsum dolor sit amet, consectetur adipisicing elit';

var positions=new Array();

var pos=str.indexOf(e);

while(pos>-1){

  positions.push(pos);

  pos=str.indexOf('e',pos+1);

}

 

4) trim()创建一个字符串副本,删除前置及后缀的所有空格,然后返回结果,不影响原始数据。

 

5)toLowerCase() toLocalLowerCase() toUpperCase() toLocalUpperCase()

 

6)字符串的模式匹配方法

1.match()接受一个参数,要么是一个正则表达式,要么是一个RegExp对象。

var text='cat, bat, sat, fat';
var pattern=/.at/;
var matches=text.match(pattern);
alert(matches.index);//0
alert(matches.input);//cat, bat, sat, fat
alert(matches[0]);//cat
alert(matches.lastIndex);//0

match()返回了一个数组,同RegExp对象的exec()方法。

 

2.search() 参数同match(),返回字符串中第一个匹配项的索引,如果没有找到匹配项,则返回-1;

var text='cat, bat, sat, fat';

var pos=text.search(/at/);

alert(pos)//1

 

3.replace()方法

接受两个参数:第一个参数可以是一个RegExp对象或者一个字符串(这个字符串不会被转换成正则表达式),第二个参数可以是一个字符串或者一个函数。如果第一个参数是字符串,那么只会替换第一个子字符串。要想替换所有子字符串,唯一的办法就是提供一个正则表达式,而且要指定全局g标志。

var text='cat, bat, sat, fat';

var result=text.replace('at','ond');// cond, bat, sat, fat

result=text.replace(/at/g,'ond')//cond bond sond fond

 

replace()方法的第二个参数还可以是一个函数。在只有一个匹配项(即与模式匹配的字符串)的情况下,会向这个函数传递3个参数:模式的匹配项,模式匹配项在字符串中的位置,原始字符串。在正则表达式中定义了多个捕获组的情况下,传递给函数的参数依次是模式的匹配项,第一个捕获组的匹配项,第二个捕获组的匹配项。。。,但最后两个参数仍然分别是模式的匹配项在字符串中的位置和原始字符串。这个函数应该返回一个字符串,表示应该被替换的匹配项。

name = 'aaa bbb ccc';

uw=name.replace(/\b\w+\b/g, function(word){   //尽力3次循环,第一次word为aaa,第二次word为bbb,第三次word为ccc

   return word.substring(0,1).toUpperCase()+word.substring(1);

} );

输出Aaa Bbb Ccc

4) split()基于指定的分隔符分隔成多个子字符串,并将结果放在一个数组中。分隔符可以是字符串,也可以是一个RegExp对象。这个方法可以接受可选的第二个参数,用于指定数组的大小,以便确保返回的数组不会超过既定大小。

5)localCompare()比较两个字符串

 

6)fromCharCode()方法接受字符编码返回字符串。

posted @ 2016-04-18 21:38  爆炒小黄鸡  阅读(200)  评论(0编辑  收藏  举报