JavaScript 高级程序设计----String类型

String类型

String 类型是字符串的对象包装类型,可以像下面这样使用 String 构造函数来创建。

var stringObject = new String("hello world");

基本的方法:继承的 valueOf()、toLocaleString()和 toString(),还有length。

记录点 1 即使字符串中包 含双字节字符(不是占一个字节的 ASCII 字符),每个字符也仍然算一个字符。

字符方法

charAt()和 charCodeAt()。

var stringValue = "hello world";
alert(stringValue.charAt(1));   //"e"

var stringValue = "hello world";
alert(stringValue.charCodeAt(1)); //输出"101"

var stringValue = "hello world";
alert(stringValue[1]);   //"e"
字符串操作方法

concat(),slice(),substr(),substring()

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

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"

在传递给这些方法的参数是负值的情况下,它们的行为就不尽相同了。

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));    //""(空字符串)

其中,slice()方法会将传 入的负值与字符串的长度相加,substr()方法将负的第一个参数加上字符串的长度,而将负的第二个 参数转换为 0。最后,substring()方法会把所有负值参数都转换为 0。
当第二个参数是负值时,slice()方法会把第二个参数转换为 7,这 就相当于调用了 slice(3,7),因此返回"lo w"。substring()方法会把第二个参数转换为 0,使调 用变成了 substring(3,0),而由于这个方法会将较小的数作为开始位置,将较大的数作为结束位置, 因此最终相当于调用了 substring(0,3)。substr()也会将第二个参数转换为 0,这也就意味着返回 包含零个字符的字符串,也就是一个空字符串。

字符串位置方法

indexOf(),lastIndexOf()

这两个方法都可以接收可选的第二个参数,表示从字符串中的哪个位置开始搜索。

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

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

大小写转换
var stringValue = "hello world";

alert(stringValue.toLocaleUpperCase());  //"HELLO WORLD"
alert(stringValue.toUpperCase());        //"HELLO WORLD"
alert(stringValue.toLocaleLowerCase());  //"hello world"
alert(stringValue.toLowerCase());        //"hello world"
模式匹配

match()

在字符串上调用这个方法,本质上与调用 RegExp 的 exec()方法相同。
方法只接受一个参数,要么是一 个正则表达式,要么是一个 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()方法的参数相同:由字 符串或 RegExp 对象指定的一个正则表达式。search()方法返回字符串中第一个匹配项的索引;如果没 有找到匹配项,则返回-1。

replace()

这个方法接受两个参数:第 一个参数可以是一个 RegExp 对象或者一个字符串(这个字符串不会被转换成正则表达式),第二个参 数可以是一个字符串或者一个函数。

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"

如果第二个参数是字符串,那么还可以使用一些特殊的字符序列,将正则表达式操作得到的值插入 到结果字符串中。

字符序列替换文本
$$$
$&匹配整个模式的子字符串。与RegExp.lastMatch的值相同
$’匹配的子字符串之前的子字符串。与RegExp.leftContext的值相同
$`匹配的子字符串之后的子字符串。与RegExp.rightContext的值相同
$n匹配第n个捕获组的子字符串,其中n等于0~9。例如,$1是匹配第一个捕获组的子字符串,$2是匹配第二个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串
$nn匹配第nn个捕获组的子字符串,其中nn等于01~99。例如,$01是匹配第一个捕获组的子字符串,$02是匹配第二个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串
var text = "cat, bat, sat, fat";
result = text.replace(/(.at)/g, "word ($1)");
alert(result);    //word (cat), word (bat), word (sat), word (fat)

replace()方法的第二个参数也可以是一个函数。

情况1:在只有一个匹配项(即与模式匹配的字符串)的 情况下,会向这个函数传递 3 个参数:模式的匹配项、模式匹配项在字符串中的位置和原始字符串。
情况2:在正则表达式中定义了多个捕获组的情况下,传递给函数的参数依次是模式的匹配项、第一个捕获组的匹 配项、第二个捕获组的匹配项…,但最后两个参数仍然分别是模式的匹配项在字符串中的位置和原始字符串。

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

split()

  1. 这个方法可以基于指定的分隔符将一个字符串分割成 多个子字符串,并将结果放在一个数组中。
  2. 分隔符可以是字符串,也可以是一个 RegExp 对象(这个方 法不会将字符串看成正则表达式)。
  3. split()方法可以接受可选的第二个参数,用于指定数组的大小, 以便确保返回的数组不会超过既定大小。
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(",");     //["red", "blue", "green", "yellow"]
var colors2 = colorText.split(",", 2);  //["red", "blue"]
var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""]

注:对 split()中正则表达式的支持因浏览器而异。

localeCompare()

这个方法比较两个字符串,并返回下列 值中的一个:

  1. 如果字符串在字母表中应该排在字符串参数之前,则返回一个负数(大多数情况下是-1,具体 的值要视实现而定);
  2. 如果字符串等于字符串参数,则返回 0;
  3. 如果字符串在字母表中应该排在字符串参数之后,则返回一个正数(大多数情况下是 1,具体的值同样要视实现而定)。
fromCharCode()

String 构造函数本身的一个静态方法。

alert(String.fromCharCode(104, 101, 108, 108, 111)); //"hello"
HTML 方法

(略)

至此,结束。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值