5.4 引用类型之 string

1.字符方法

charAt() 和charCodeAt() ,这两个方法都接收一个参数,即基于0的字符位置。其中charAt()是以单字符字符串形式返回给定位置的那个字符。charCodeAt()是返回字符编码。

var str = 'tyt';
console.log(str.charAt(1));//y 
console.log(str.charCodeAt(1));//121

ES5中可以使用[ ]加数字索引来访问特定字符,但在IE7中会返回undefined。

2.字符串操作方法

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

var str = 'tyt';
var result = str.concat("小屠");
console.log(result);//tyt小屠
console.log(str);//tyt

ES5还提供了三个基于子字符串创建新字符串的方法: slice() substr() substring().这三个方法都返回被操作字符串的一个子字符串,接收一个或两个参数。第一个参数指定子字符串的
开始位置,第二个参数(在指定情况下)表示子字符串到哪里结束,slice() substr()的第二个参数指定的是子字符串最后一个字符后面的而为之。而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"
3.字符串位置方法

indexOf() lastIndexOf() ;他们都是从一个字符串中搜索给定的子字符串,返回子字符串的位置,么有找到返回-1,indexOf从前向后搜索,lastIndexOf从后向前搜索。

var stringValue = "hello world";
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7
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(positions); //"3,24,32,35,52"
4. trim () 方法:

这个方法会创建一个字符串的副本,删除前置和后缀的所有空格。

var str =  '   tytabc  ';
var result = str.trim();
console.log(result);// ”tytabc‘’

Firefox 3.5+、 Safari 5+和 Chrome 8+还支持非标准的 trimLeft()和 trimRight()方法,分别用于删除字符串开头和末尾的空格

5.字符串大小写转换方法:

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

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的exec() 方法相同,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

本例中的 match()方法返回了一个数组;如果是调用 RegExp 对象的 exec()方法并传递本例中的
字符串作为参数,那么也会得到与此相同的数组:数组的第一项是与整个模式匹配的字符串,之后的每
一项(如果有)保存着与正则表达式中的捕获组匹配的字符串
还有一个search() replace() 方法。

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

要想替
换所有子字符串,唯一的办法就是提供一个正则表达式,而且要指定全局(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"

最后一个与模式匹配有关的方法是 split(),这个方法可以基于指定的分隔符将一个字符串分割成多个子字符串,并将结果放在一个数组中。分隔符可以是字符串,也可以是一个 RegExp 对象(这个方法不会将字符串看成正则表达式)。 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(/[^\,]+/); //["", ",", ",", ",", ""]

7.fromCharCode() 静态方法

接收一或多个字符编码,将他们转换成一个字符串;

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

8.Global 对象

URL编码方法:
encodeURL() 和 encodeURLComponent() 可以对URL进行编码,以便发送给浏览器,有限的URL里不能包含某些字符;
encodeURI()主要用于整个 URI(例如, http://www.wrox.com/illegal value.htm),而 encodeURIComponent()主要用于对 URI 中的某一段(例如前面 URI 中的 illegal value.htm)进行编码。它们的主要区别在于, encodeURI()不会对本身属于 URI 的特殊字符进行编码,例如冒号、正斜杠、问号和井字号;而 encodeURIComponent()则会对它发现的任何非标准字符进行编码。来看下面的例子。

var uri = "http://www.wrox.com/illegal value.htm#start";
//"http://www.wrox.com/illegal%20value.htm#start"
alert(encodeURI(uri));
//"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start"
alert(encodeURIComponent(uri));

一 般 来 说 , 我 们 使 用 encodeURIComponent() 方 法 的 时 候 要 比 使 用encodeURI()更多,因为在实践中更常见的是对查询字符串参数而不是对基础 URI进行编码。

发布了18 篇原创文章 · 获赞 44 · 访问量 3684

猜你喜欢

转载自blog.csdn.net/qq_44758322/article/details/105600439
5.4