打卡时间:15:00
String类型
5、字符串大小写转换方法
ECMAScript中设计字符串大小写转换的方法有4个:toLowerCase( )、toLocaleLowerCase( )、toUpperCase( )和toLocaleUpperCase( )。
其中,toLowerCase()和toUpperCase()是两个经典的方法,借鉴自java.lang.String中的同名方法
toLocaleLowerCase( )和toLocaleUpperCase( )是针对特定地区的实现。
var stringValue = "hello world";
alert(stringValue.toLocaleUpperCase());//HELLO WORLD
alert(stringValue.toUpperCase());//HELLO WORLD
alert(stringValue.toLocaleLowerCase());//hello world
alert(stringValue.toLowerCase());//hello world
6、字符串的模式匹配方法
String类型定义了几个用于在字符串中匹配模式的方法。
◆ match() 方法只接受一个参数,要么是一个正则表达式,要么是一个RegExp对象。例:
var text = "cat , bat, sat , fat ;"
var pattern= /.at/;
var matches = text.match (pattern);
alert( matches.index);//0
alert( matches[0]);//cat
alert( pattern.lastIndex);//0
◆ search( )方法唯一参数与match( )方法的参数相同:由字符串或RegExp对象指定的一个正则表达式。
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
如果第二个参数是字符串,那么可以使用一些特殊的字符序列,将正则表达式操作得到的值插入到结果字符串中。
例:
var text = "cat , bat, sat , fat ;"
result = text.replace(/(.at)/g,"word($1)");
alert(result);//word(cat),word(bat),word(sat),word(fat)
◆ split()方法可以基于制定的分隔符将一个字符串分隔成多个子字符串,并将结果放在一个数组中。分隔符可以是字符串,也可以是一个RegExp对象。split()方法可以接受可选的第二个参数,用于制定数组的大小,以便确保返回的数组不会超过既定大小。例:
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(",");
var colors2 = colorText.split("," , 2);
var colors3 = colorText.split(/[^\,]+/);
7、localeCompare()方法
这个方法比较两个字符串,并返回下列字符串中的一个:
● 如果字符串在字母表中应该排在字符串参数之前,则返回一个负数(大多数情况下是-1)
● 如果字符串等于字符串参数,则返回0;
● 如果字符串在字母表中应该排在字符串参数之后,则返回一个正数(大多数情况下是1)
var stringValue = "yellow";
alert(stringValue.localeCompare("brick"));//1
alert(stringValue.localeCompare("yellow"));//0
alert(stringValue.localeCompare("zoo"));//-1
localeCompare( )方法比较与众不同的地方就是实现所支持的地区(国家和语言)决定了这个方法的行为。比如美国以英语作为ECMAScript实现的标准语言,因此localeCompare()就是区分大小写的,于是大写字母在字母表中排在小写字母前头就成为了一项决定性的比较规则。
因为localeCompare( )返回的数值取决于实现,所以最好是像下面例子所示的使用这个方法:
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");
8、fromCharCode()方法
String构造函数本身还有一个静态方法:fromCharCode()。这个方法的任务是接受一或者多个字符编码,然后将他们转换成一个字符串。从本质上看,这个方法与实例方法charCodeAt()执行的是相反的操作,例:
alert(String.fromCharCode(104,101,108,108,111));//hello
//这里传递的是字符串hello中每个字母的字符编码
9、HTML方法
早期的web浏览器提供商察觉到使用Javascript动态格式化HTML的需求。于是就扩展了标准,实现了一些专门用于简化常见HTML格式化任务的方法。下表列出了这个HTML方法。但是尽量不使用这些方法,因为它们创建的标记通常无法表达语义。