字符串方法

2018-08-08  本文已影响0人  麓语

字符方法

charAt()

接受一个参数:基于0的字符位置
返回值:指定位置的字符

var str = 'hello world';
console.log(str.charAt(1));                     // => 'e'
charCodeAt()

接受一个参数:基于0的字符位置
返回值:指定位置字符的字符编码

var str = 'hello world';
console.log(str.charCodeAt(1));                // => 101
ECMAScript 5 访问个别字符方法

使用方括号加数字索引来访问字符串中特定字符

var str = 'hello world';
console.log(str[1])                           // => 'e'

字符串操作方法

concat()

可以接受任意多个参数,用于一个或多个字符串拼接起来

var str = 'hello ';
var resultStr = str.concat('world', '!');
console.log(resultStr);                      // => 'hello world!'
console.log(str);                            // => 'hello '
slice() 、substr() 、substring()

参数1:指定子字符串的开始位置
参数2:(可选)在指定的情况下,表示字符串到哪里结束
slice()substring()的第二个参数指定的是子字符串最后一个字符后面的位置,substr()的第二个参数是指定返回的字符个数
这三个方法都不会修改原字符串的值,只是返回一个基本类型的字符串值

var str = 'hello world';
console.log(str.slice(3));                    // => 'lo world'
console.log(str.substring(3));                // => 'lo world'
console.log(str.substr(3));                   // => 'lo world'
console.log(str.slice(3 ,7));                 // => 'lo w'
console.log(str.substring(3, 7));             // => 'lo w'
console.log(str.substr(3, 7));                // => 'lo worl'

在传递参数存在负值的情况下:
slice()会将传入的负值与字符串的长度相加;
substr()会将负的第一个参数与字符串的长度相加,将负的第二个参数转换为0

substring()会将所有负值参数都转化为0

var str = 'hello world';
console.log(str.slice(-3));                    // => 'rld'
console.log(str.substring(-3));                // => 'hello world'
console.log(str.substr(-3));                   // => 'rld'
console.log(str.slice(3 ,-4));                 // => 'lo w'
console.log(str.substring(3, -4));             // => 'hel'
console.log(str.substr(3, -4));                // => ''

字符串位置方法

indexOf()、lastIndexOf()

从字符串中检索给定的子字符串,然后返回子字符串的位置(如果没有找到该子字符串,则返回-1
indexOf()从字符串的开头向后搜索子字符串;
lastIndexOf()从字符串末尾向前搜索子字符串

var str = 'hello world';
console.log(str.indexOf('o'));                  // => 4
console.log(str.lastIndexOf('o'));              // => 7

这两个方法都可以接受第二个参数:表示从字符串中的哪个位置开始搜索
indexOf()从指定的位置向后搜索子字符串;
lastIndexOf()从指定的位置向前搜索子字符串

var str = 'hello world';
console.log(str.indexOf('o', 6));                  // => 7
console.log(str.lastIndexOf('o', 6));              // => 4

trim()方法

ECMAScript 5 为字符串定义了trim()方法:创建一个字符串的副本,删除前置和后缀的所有空格

var str = '     hello world     ';
var resultStr = str.trim();
console.log(str);                    // => '     hello world     '
console.log(resultStr);              // => 'hello world'

Chrome 8+ 支持trimLeft()trimRight(),分别用于删除开头和结尾的空格

字符串大小写转化方法

toLowerCase()、toLocaleLowerCase()、toUpperCase()、toLocaleUpperCase()
var str = 'hello world';
console.log(str.toLocaleUpperCase());          // => 'HELLO WORLD'
console.log(str.toUpperCase());                // => 'HELLO WORLD'
var str2 = 'HELLO WORLD';
console.log(str2.toLocaleLowerCase());         // => 'hello world'
console.log(str2.toLowerCase());               // => 'hello world'

字符串的模式匹配方法

match()

只接受一个参数:要么是一个正则表达式,要么是一个RegExp对象

var str = 'cat, bat, sat, fat';
var reg = /.at/;
var matches = str.match(reg);
console.log(matches.index);                  // => 0
console.log(matches[0]);                     // => 'cat'
console.log(reg.lastIndex);                  // => 0

这里当正则匹配全局模式时matches结果就是一个普通数组,包含所有匹配项的数组,不带有index等属性

search()

参数: 和match()方法参数相同:由字符串或RegExp对象指定的一个正则表达式
返回值: 字符串中第一个匹配项的索引,如果没有匹配项就返回-1

var str = 'cat, bat, sat, fat';
var reg = /at/;
var pos = str.search(reg);
console.log(pos);                          // => 1
replace()

参数1:

上一篇 下一篇

猜你喜欢

热点阅读