String 对象用于存储字符串数据, 这里我们做了 JavaScript 的 String 字符串对象常用操作总结, 需要的朋友可以参考下
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
创建 String 对象方式
声明:String 对象的方法也可以在所有基本字符串值中访问到。
调用构造函数 String():
- var str = new String();
- var str = new String('hello world');//初始化str,str.length = 11;
String 访问及查找的方式
1. 访问 (通过索引)
(1)charAt() 或 []
1 个参数, 参数为字符位置, 返回字符
- var strValue = new String('hello world');
- console.log(strValue.charAt(1));//e
- console.log(strValue[1]);//e,IE7及以下版本使用这种方式,会返回undefined
(2)charCodeAt()
1 个参数, 参数为字符位置, 返回字符编码
- var strValue = new String('hello world');
- console.log(strValue.charCodeAt(1));//101
2. 查找位置
(1)indexOf()
第一个参数为指定子字符串,第二个参数为检索位置。返回索引,如果没有找到则返回 - 1
- var str = 'hello world'
- str.indexOf('l');//2,返回找到的第一个字符的位置
- str.indexOf('l',6);//9
(2)lastIndexOf()
与 indexOf() 的区别在于,lastIndexOf() 是从字符串的末尾向前搜索子字符串
字符方法
1. 扩展字符串
concat()
接受任意数量参数,用于将一个或多个字符串拼接起来,返回拼接得到新的字符串副本。
- var str = new String('hello');
- var result = str.concat(' world');
- console.log(result);//hello world
- typeof result//"string"
2. 获取子字符串方法
slice(),substr(),substring(),这三个方法都会返回被操作字符串的子字符串副本,而且也都接受 1 或 2 个参数,前闭后开 [)
(1)slice()
- var str = 'hello';
- str.slice(0,2);//"he",第一个参数指定字符串开始的位置,第二个参数表示字符串到哪里结束
- str.slice(-3);//"llo",o代表-1,依次倒数,-3代表倒数第三个的l
- str.slice(-2,-1);//"l",同理,-2代表倒数第二个l,-1代表倒数第一的o
(2)substring()
- var str = 'hello';
- str.substring(0,2);//"he",此时的参数意义同str.slice(0,2)
- str.substring(-3);//"hello",substring()方法会把所有负值参数转换为0
- str.substring(-3,-2);//"",同上
(3)substr()
- var str = 'hello';
- str.substr(1,2);//"el",第一个参数指定字符串的开始位置,第二个参数指定的则是返回的字符个数
- str.substr(-3);//"llo",此时的参数意义同str.slice(-3)
- str.substr(-3,-1);//"",substr()方法会将负的第二个参数转换为0
substr() 方法传递负值时在 IE 中存在问题,它会返回原始的字符串,IE9 修复了这个问题
3. 将字符串转换为数组
split()
基于指定的分隔符 (可以是字符串,也可以是 RegExp 对象) 将字符串分割成多个子字符串,并将结果放在一个数组中,可接受可选的第二个参数,用于指定数组的大小,返回数组。
- var color = 'blue,red,orange';
- color.split();//["red,blue,orange"],长度为1
- color.split(',');//["blue", "red", "orange"],长度为3
- var color = 'blue-red-orange';
- color.split('-');//["blue", "red", "orange"],长度为3
- color.split(',',2);//["blue", "red"]
4. 字符串大小写转换
toLowerCase(),toUpperCase()
- var str = 'hello';
- str.toUpperCase();//"HELLO"
- str.toLowerCase();//"hello"
5. 删除字符串空格方法
trim()
删除字符串中前置以及后缀的所有空格,然后返回结果副本。
- var str = ' hello world ';
- str.trim()//"hello world"
6. 字符串的模式匹配方法
(1)match()
参数: 只接受一个参数,要么是一个正则表达式,要么是一个 RegExp() 对象。
返回: 数组。数组中的第一项是与整个模式匹配的字符串,之后的每一项 (如果有) 保存着正则表达式捕获组匹配的字符串
本质上与调用 exec() 相同。
- var text = 'cat, bat, sat, fat';
- var pattern = /.at/;
- var matches = text.match(pattern);
- matches // ["cat"]
- matches.input // "cat, bat, sat, fat"
- matches.index // 0
(2)search()
参数: 与 match() 方法相同。
返回: 字符串中第一个匹配项的索引,如果没有匹配项,则返回 - 1。
search() 方法始终从前向后找
- var text = 'cat, bat, sat, fat';
- var pattern = /at/;
- text.search(pattern) // 1
(3)replace()
参数: 接收两个参数,第一个参数可以是一个 RegExp 对象或者一个字符串 (这个字符串不会转换成正则表达式),第二个参数可以是一个字符串或者一个函数。
如果 第一个参数是字符串,那么只会替换第一个子字符串。要想替换所有子字符串,唯一的办法就是提供一个正则表达式,而且要指定全局标志 (g) 标志。
如果 第二个参数是字符串,那么还可以使用一些特殊的字符序列,将正则表达式操作得到的值插入到结果字符串中。
也可以是函数,传递给函数的参数依次是模式的匹配项,模式的匹配项在字符串中的位置,和原始字符串。在正则表达式定义了多个捕获组的情况下,传递给函数的参数依次是模式的匹配项,第一个捕获组的匹配项,以此类推,但最后两个参数分别是模式的匹配项在字符串中的位置和原始字符串。
字符序列 | 替换文本 |
---|---|
$$ | $ |
$& | 匹配整个 pattern 中的子字符串 |
$' | 匹配的子字符串之后的子字符串 |
$` | 匹配的字符串之前的子字符串 |
$n | 匹配第 n 个捕获组中的子字符串,$1 |
$nn | 匹配第 nn 个捕获组的子字符串,$01 |
- var text = 'xxx-love-xxx';
- var pattern = /xxx/g;
- var result = text.replace(pattern,'2')
- result// "2-love-2"
- text.replace(/(xxx)-\w{4}-(xxx)/g,'I love YOU');//"I love YOU"
- var text = 'xxx-love-xxx';
- var pattern1 = /xxx/g;
- var result = text.replace(pattern1,'$$')
- result// "$-love-$"
- var result = text.replace(pattern1,'$&2')
- result//"xxx2-love-xxx2"
- var result = text.replace(pattern1,'$\'')
- result//"-love-xxx-love-"
来源: http://www.phperz.com/article/17/0226/265432.html