一、相关概念
String
类型是字符串的对象包装类型String
对象var stringObject=new String("hello world");
String
对象大方法也可以在所有基本的字符串值中访问到valueOf()
、toLocaleString()
和toString()
方法,都返回对象所表示的基本字符串值二、属性
String
类型的每个实例都有一个length
属性,表示字符串中包含多个字符ASCII
字符),每个字符仍然算一个字符var stringValue=new String("hello world");
alert(stringValue.length);/"11"
三、方法
var stringValue=new String("hello world");
alert(stringValue.charAt(1));//"e"
var stringValue=new String("hello world");
alert(stringValue.charCodeAt(1));//"101"
IE8
及Firefox
、Safari
、Chrome
和Opera
所有版本的支持IE7
及更早版本中使用这种语法,会返回undefined
值(尽管根本不是特殊的undefined
值)var stringValue=new String("hello world");
alert(stringValue[1]);//"e"
concat()
用于将一或多个字符串拼接起来,返回拼接得到的新字符串。不改变原字符串concat()
方法可以接收任意多个参数,也就是说可以通过它拼接任意多个字符串concat()
是专门用来拼接字符串的方法,但实践中使用更多的还是加号操作符(+)。而且,使用加号操作符在大多数情况下都比使用concat()
方法要简便易行(特别是在拼接多个字符串的情况下)var stringValue=new String("hello ");
var result=stringValue.concat("world");
alert(result);//"hello world"
alert(stringValue);//"hello "
var stringValue=new String("hello ");
var result=stringValue.concat("world","!");
alert(result);//"hello world!"
alert(stringValue);//"hello "
slice()
会将传入的负值与字符串的长度相加var stringValue=new String("hello world");
alert(stringValue.slice(3));//"lo world"
alert(stringValue.slice(3,7));//"lo w"
var stringValue=new String("hello world");
alert(stringValue.slice(-3));//"rld"
alert(stringValue.slice(3,-4));//"lo w"
substr()
方法会将负的第一个参数加上字符串的长度,而将将负的第二个参数转换为0var stringValue=new String("hello world");
alert(stringValue.substr(3));//"lo world"
alert(stringValue.substr(3,7));//"lo worl"
var stringValue=new String("hello world");
alert(stringValue.substr(-3));//"rld"
alert(stringValue.substr(3,-4));//""
//substr()会将第二个参数转换为0,返回的字符串包含零个字符串,也就是一个空字符串
substring()
方法会将所有的负值参数都转换为0var stringValue=new String("hello world");
alert(stringValue.substring(3));//"lo world"
alert(stringValue.substring(3,7));//"lo w"
var stringValue=new String("hello world");
alert(stringValue.substring(-3));//"hello world"
alert(stringValue.substring(3,-4));//"hel"
//substring()方法会把第二个参数转换为0,也就是substring(3,0),而由于这个方法会将较小的数作为开始位置,将较大数作为结束位置,最终相当于调用了substring(0,3)
indexOf()
方法从字符串的开头向后搜索字符串indexOf()
和lastIndexOf()
会返回相同的位置值indexOf()
会从该参数指定的位置向后搜索,忽略该位置之前的所有字符var stringValue=new String("hello world");
alert(stringValue.indexOf("o"));//4
var stringValue=new String("hello world");
alert(stringValue.indexOf("o",6));//7
var strinngValue="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(positions);
lastIndexOf()
方法是从字符串的末尾向前搜索字符串lastIndexOf()
会从该参数指定的位置向前搜索,忽略该位置之后的所有字符var stringValue=new String("hello world");
alert(stringValue.lastIndexOf("o"));//7
var stringValue=new String("hello world");
alert(stringValue.lastIndexOf("o",6));//4
trim()
会创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果trim()
返回的是字符串的副本,所以原始字符串中的前置及后缀空格会保持不变IE 9+
、Firefox 3.5+
、Safari 5+
、Opera 10.5+
和Chrome
Firefox 3.5+
、Safari 5+
和Chrome 8+
还支持非标准的trimLeft()
和trimRight()
方法,分别用于删除字符串开头和末尾的空格var stringValue=new String(" hello world ");
var trimmedStringValue=stringValue.trim();
alert(stringValue);" hello world "
alert(trimmedStringValue);//"hello world"
java.lang.String
中的同名方法,将对象转换为小写var stringValue=new String("hello world");
alert(stringValue.toLowerCase());//"hello world"
var stringValue=new String("hello world");
alert(stringValue.toLocaleLowerCase());//"hello world"
java.lang.String
中的同名方法,将对象转换为大写var stringValue=new String("hello world");
alert(stringValue.toUpperCase());//"HELLO WORLD"
var stringValue=new String("hello world");
alert(stringValue.toLocaleUpperCase());//"HELLO WORLD"
match()
,在字符串上调用这个方法,本质上与调用RegExp()
的exec()
方法相同match()
方法只接收一个参数,要么是一个正则表达式,要么是一个RegExp
对象var text=new String("cat,bat,sat,fat");
var pattern=/.at/;
//与pattern.exec(text)相同
var matches=text.match(pattern);
alert(matches.index);//0
alert(matches[0]);
alert(pattern.lastIndex);//0
//match()方法返回了一个数组
//如果是调用RegExp对象的exec()方法并传递本例中的字符串作为参数,那么也会得到与此相同的数组:数组的第一项是与整个模式匹配的字符串,之后的每一项(如果有)保存着与正则表达式中的捕获组匹配的字符串
search()
方法的唯一参数与match()
方法的参数相同:由字符串或RegExp
对象指定的一个正则表达式search()
方法返回字符串中第一个匹配项的索引;如果没有找到匹配项,则返回-1search()
方法始终是从字符串开头向后查找模式var text=new String("cat,bat,sat,fat");
var pos=text.search(/at/);
alert(pos);//1
//search()方法返回1,即"at"在字符串中第一次出现的位置
replace()
用于替换字符串RegExp
对象或者一个字符串(这个字符串不会被转换成正则表达式),第二个参数可以是一个字符串或者一个函数(g)
标志replace()
方法的第二个参数也可以是一个函数。在只有一个匹配项(即与模式匹配的字符串)的情况下,会向这个函数传递3个阐述:模式匹配、模式匹配项在字符串中的位置和原始字符串。在正则表达式中定义了多个捕获组的情况下,传递给函数的参数依次是模式的匹配项、第一个捕获组的匹配项、第二个捕获组的匹配项······,但最后两个参数仍然分别是模式的匹配项在字符串中的位置和原始字符串。这个函数应该返回一个字符串,表示应该被替换的匹配项。使用函数作为replace()
方法的第二个参数可以实现更加精细的替换操作function htmlEscape(text){
return text.replace(/[<>"&]/g,function(match,pos,originalText){
switch(match){
case "<":return "<";
case ">":return "↦";
case "\"":return """;
}
};)
}
alert(htmlEscape("Hello world!
"));
//<p class="greeting">Hello world!</p>
字符序列 | 替换文本 |
---|---|
$$ |
$ |
$& |
匹配整个模式的子字符串。与RegExp.lastMatch 的值相同 |
$' |
匹配的子字符串之前的字符串。与RegExp.leftContext 的值相同 |
$` | 匹配的子字符串之后的子字符串。与RegExp.rightContext的值相同 |
$n |
匹配第n个捕获组的子字符串,其中n等于0~9.例如,$1是匹配第一个捕获组的子字符串,$2是匹配第二个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串 |
$nn |
匹配第nn个捕获组的子字符串,其中nn等于01~99。例如,$01是匹配第一个捕获组的子字符串,$02是匹配第二个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串 |
var text=new String("cat,bat,sat,fat");
var result=text.replace("at","ond");
alert(result);//"cond,bat,sat,fat"
//传入replace()方法的是字符串"at"和替换用的字符串"ond"。替换的结果是把"cat"变成了"cond",但字符串中的其他字符并没有收到任何影响
result=text.replace(/at/g,"ond");
alert(result);//"cond,bond,sond,fond"
//通过将第一个参数修改为带有全局标志的正则表达式,就将全部"at"都替换成了"ond"
var text=new String("cat,bat,sat,fat");
result=text.replace(/(.at)/g,"word($1)");
alert(result);//word(cat),word(bat),word(sat),word(fat)
//在此,每个以"at"结尾的单词都被替换了,替换结果是"word"后跟一对圆括号,而圆括号中是被字符序列$1所替换的单词
split()
方法可以基于指定的分隔符将一个字符串分隔成多个字符串,并将结果放在一个数组中。分隔符可以是字符串,也可以是一个RegExp
对象(这个方法不会将字符串看成正则表达式)split()
方法可以接受可选的第二个参数,用于指定数组的大小,以便确保返回的数组不会超过既定大小split()
正正则表达式的支持因浏览器而异。尽管对于简单的模式没什么差别,但对于未发现匹配项及带有捕获组的模式,匹配的行为大不相同
IE8
及之前版本会忽略捕获组。ECMA-262规定应该把捕获组拼接到结果数组中。IE9能正确地在结果中包含捕获组Firefox 3.6
及之前版本在捕获组未找到匹配项时,会在结果数组中包含空字符串;ECMA-262规定没有匹配项的捕获组在结果数组中应该用undefined
表示var colorText=new String("red,blue,green,yellow");
var colors1=colorText.split(",");//["red","blue","green","yellow"]
var colors1=colorText.split(",",2);//["red","blue"]
var colors1=colorText.split(/[^\,]+/);//["",",",",",""]
//通过正则表达式指定的分隔符出现在了字符串的的开头(即子字符串"red")和末尾(即子字符串"yellow")
localeCompare()
方法比较两个字符串,并返回下列值中的第一个
var stringValue=new String("yellow");
alert(stringValue.localeCompare("brick"));//1
alert(stringValue.localeCompare("yellow"));//0
alert(stringValue.localeCompare("zoo"));//-1
function determineOrder(value){
var result=stringValue.localeCompare(value);
if(result<0){
alert("The string 'yellow' comes before the string '"+value+"'.");
}else if(result>0){
alert("The string 'yellow' comes after the string '"+value+"'.");
}else{
alert("The string 'yellow' is equal to the string '"+value+"'.");
}
}
determineOrder("brick");
determineOrder("yellow");
determineOrder("zoo");
String
构造函数本身还有一个静态方法:fromCharCode()
。这个方法的任务是接受一或多个字符编码,然后将它们转换成一个字符串charCodeAt()
执行的是相反的操作alert(String.fromCharCode(104,101,,108,108,111));//"hello"
//给fromCharCode()传递的是字符串"hello"中每个字母的字符编码
Web
浏览器提供商觉察到了使用JavaScript
动态格式化HTML
的需求。这些提供商就扩展了标准,实现了一些专门用于简化常见HTML
格式化任务的方法方法 | 输出结果 |
---|---|
anchor(name) |
string |
big() |
string |
bold() |
string |
fixed() |
string |
fontcolor() |
string |
fontsize() |
string |
italics() |
string |
link(url) |
string |
small() |
string |
strike() |
|
sub() |
string |
sup() |
string |
源于整理《JavaScript高级程序设计》