您的位置:首页 > Web前端 > JavaScript

JS数据类型之String类型

2015-09-12 18:44 603 查看

String类型

一、字符方法

chart()、charCodeAt()

这两个方法都接收一个参数,即基于0的字符位置

var stringValue="hello world";
alert(stringValue.chartAt(1));  //"e" 得到的是字符
alert(stringValue.chartCodeAt(1));  //"101" 得到的是字符编码


二、字符串操作方法

concat()、slice()、substr()、substring()

1.concat()用于将一或多个字符串拼接起来,返回拼接得到的新字符串。

var stringValue="hello ";
var result=stringValue.concat("world");
alert(result);  //"hello world"
alert(stringValue); //"hello"


concat()方法可以接受任意多个参数,也就是说可以通过它拼接任意多个字符串。

2.ECMAScript还提供了三个基于子字符串创建新字符串的方法:slice()、substr()和substring()

这三个方法都会返回被操作字符串的一个子字符串,而且也都接收一个或两个参数。第一个参数指定子字符串的开始位置,第二个参数表示在哪里结束。具体来说,slice()和substring()的第二个参数指定的是子字符串最后一个字符后面的位置。而substr()的第二个参数指定的则是返回的字符个数。

var stringValue="Hello world";
alert(stringValue.slice(3));        //"lo world"
alert(stringValue.substring(3));    //"lo world"
alert(stringValue.substr(3));       //"lo world"
alert(stringValue.slice(3,7));      //"lo w"
alert(stringValue.substring(3,7));  //"lo w"
alert(stringValue.substr(3,7));     //"lo worl"


当参数是负数时,slice()方法会将传入的负值与字符串的长度相加。substr()方法将负的第一个参数加上字符串的长度,而将负的第二个参数转换为0。最后,substring()方法会把所有负值参数都转化为0。例子如下:

var stringValue="hello world";
alert(stringValue.slice(-3));       //"rld"
alert(stringValue.substring(-3));   //"hello world"
alert(stringValue.substr(-3));      //"rld"
alert(stringValue.slice(3,-4));     //"lo w"
alert(stringValue.substring(3,-4)); //"hel"
alert(stringValue.substr(3,-4));    //""空字符


3、字符串位置方法

indexOf()、lastIndexOf()

indexOf()方法从字符串的开头向后搜索子字符串,而lastIndexOf()方法是从字符串的末尾向前搜索子字符串。

var stringValue="hello world";
alert(stringValue.indexOf("o"));    //4
alert(stringValue.lastIndexOf("o"));    //7
alert(stringValue.indexOf("o",6));  //7
alert(stringValue.lastIndexOf("o",6));  //4


在使用第二个参数的情况下,可以通过循环调用indexOf()或lastIndexOf()来找到所有匹配的子字符串,如下面的例子所示:

var stringValue="Lorem ipsum dolor sit amet, consectetur adipisicing elit";
var positions=new Array();
var pos=stringValue.indexOf("e");

while(pos>-1){
positions.push(pos);
pos=stringValue.indexOf("e",pos+1);
}

alert(position);    //"3,24,32,35,52"


4、trim()方法

这个方法会创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果

var stringValue=" hello world ";
var trimmedStringValue=stringValue.trim();
alert(stringValue); //" hello world "
alert(trimmedStringValue);  //"hello world"


5、字符串大小写转换方法

toLowerCase()、toLocaleLowerCase()、toUpperCase()、toLocaleUpperCase()

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

match()、search()、replace()、split()

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

var text="cat, bat, sat, fat";
var pattern=/.at/;

//与pattern.exec(text)相同
var matches=text.match(pattern);
alert(matches.index);   //0
alert(matches[0]);  //"cat"
alert(pattern.lastIndex);   //0


search()方法与match()方法的参数相同。search()方法返回字符串中第一个匹配项的索引,如果没有找到匹配项,则返回-1。而且,search()方法始终是从字符串开头向后查找模式。如下:

var text="cat, bat, sat, fat";
var pos=text.search(/at/);
alert(pos); //1


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

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

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


replace()方法的第二个参数也可以是一个函数。在只有一个匹配项的情况下,会向这个函数传递3个参数:模式的匹配项、模式匹配项在字符串中的位置和原始字符串。例子如下:

function htmlEscape(text){
return text.replace(/[<>"&]/g,function(match,pos,originalText){
switch(match){
case "<" : return "<";
case ">" : return ">";
case "&" : return "&";
case "\"" : return """;
}
});
}
alert(htmlEscape("<p class=\"greeting\">Hello world!</p>"));
//<p class="greeting">Hello world!</p>


splite()方法

var colorText="red,blue,green,yellow";
var colors1=colorText.splite(","); //["red","blue","green","yellow"]
var colors2=colorText.splite(",",2); //["red","blue"]
var colors3=colorText.splite(/[^\,]+/); //["" , "," , "," , "," , ""]


需要注意的是,在最后一次调用split()返回的数组中,第一项和最后一次项是两个空字符串。之所以这样,是因为通过正则表达式指定的分隔符出现在了字符串的开头(即子字符串”red”)和末尾(即子字符串”yellow”)。

7.localeCompare()方法

如果字符串在字母表中应该排在字符串参数之前,则返回一个负数(大多数情况下是-1,具体的值要视实现而定)

如果字符串等于字符串参数,则返回0

如果字符串在字母表中应该排在字符串参数之后,则返回一个正数(大多数情况下是1,具体的值要视实现而定)

var stringValue="yellow";
alert(stringValue.localeCompare("brick"));  //1
alert(stringValue.localeCompare("yellow")); //0
alert(stringValue.localeCompare("zoo"));    //-1


8、fromCharCode()方法

这个方法的任务是接收一或多个字符编码,然后将他们转换成一个字符串

alert(String.fromCharCode(104,101,108,108,111));    //"hello"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: