js string方法

428 阅读5分钟

charAt

const str = 'abc';
console.log(str.charAt(0)) // 'a'
// 返回指定index下的字符

charCodeAt

const str = 'abc';
console.log(str.charCodeAt(0)) // '97'
// 返回0到65535之间的整数,表示给定索引处的UTF-16代码单元

concat

const str = 'abc';
str.concat('def'); // 'abcdef';
const arr = ['hello', 'world'];
str.concat([...arr]); // abchelloworld

说明:concat并不会改变源字符串
性能:强烈建议使用赋值操作符+ +=代替concat方法 MDN说明

endsWith

const str = 'abc';
str.endsWith('c', 3)
// 方法用来判断当前字符串是否是以另外一个给定的字符串结尾的,返回true或者false
// 第二个参数是str的长度,默认为str.length

String.formCharCode

console.log(String.formCharCode(97,98,99,100)) // 'abcd';
// 静态方法,返回由指定的UTF-16代码单元序列的创建的字符串;

includes

const str = 'abc';
str.includes('a'); // true
// 该方法用于判断一个字符串是否包含再另一个字符串中,返回true或false;

IE不支持此方法,用indexOf代替

indexOf

const str = 'abc';
str.indexOf('b'); // 1;
// 方法返回给定字符串在str中首次出现的索引,如果未找到该值,则返回-1;

lastIndexOf

const str = 'abcdeacgv';
str.lastIndexOf('a'); // 5
// 方法返回给定字符串在str中最后一次出现的索引,如果未找到该值,则返回-1;

用于操作正则的字符串方法

共有4个操作字符串的方法

match, search, split, replace

searchmatch 如果参数不是正则,则会将字符串转为正则对象, 使用这两种方法,最好使用正则的写法,以免写更多的\

match返回结果的格式问题:与正则对象是否有g标识有关
g的情况下,返回的是所有符合正则匹配的字符, 没有g的情况下,返回的是标准匹配的内容

match

如下: 
var string = "2017.06.27"; var regex1 = /\b(\d+)\b/; 
var regex2 = /\b(\d+)\b/g; 
console.log( string.match(regex1) ); 
console.log( string.match(regex2) );
// => ["2017", "2017", index: 0, input: "2017.06.27"] 
// => ["2017", "06", "27"]

exec循环

// exec比mantch更强大
// match加上g标识符后,获取不到下标的关键信息, exec可以解决这个问题,它能接着上一次匹配后,继续匹配;
var string = "2017.06.27";
var regex2 = /\b(\d+)\b/g;
var result;
while ( result = regex2.exec(string) ) {
  console.log( result, regex2.lastIndex );
}

// 可以使用while循环来解决这个问题;

matchAll

js Api 新的match方法的变种,在它没有出现之前,经常使用上面的写法,来获取所有的匹配信息;
使用`matchAll` 就不需要使用while循环加exec的方式,可以更方便的实现功能;
// 使用matchALL必须加上`g`标识
var regexp = /t(e)(st(\d?))/g;
var str = 'test1test2';
let arr = [...str.matchAll(regexp)];
arr[0]
//  ['test1', 'e', 'st1', '1', index: 0, input: 'test1test2', groups: undefined]
arr[1]
// ['test2', 'e', 'st2', '2', index: 5, input: 'test1test2', groups: undefined]

eplace

replace 是最强大的字符串操作函数,它的第一个值可以是一个字符串,也可以是一个正则表达式,第二个参数可以字符串或者是一个每次匹配都调用的函数;

精确的参数个数依赖于replace()的第一个参数是否是一个正则表达式对象,以及这个正则表达式中指定了多少个括号子串

"1234 2345 3456".replace(/(\d)\d{2}(\d)/g, function (match, $1, $2, index, input) 
{   
    console.log([match, $1, $2, index, input]); 
}); 
// => ["1234", "1", "4", 0, "1234 2345 3456"] 
// => ["2345", "2", "5", 5, "1234 2345 3456"] 
// => ["3456", "3", "6", 10, "1234 2345 3456"]

match 是每次正则匹配所匹配到的数据,$1-$99是每个正则里面的捕获组所匹配到的数据,index 是下标,input是调用该方法的str

padEnd

const str1 = 'Breaded Mushrooms';
console.log(str1.padEnd(25, '.'));
// output: "Breaded Mushrooms........"
// 方法会用一个字符串填充当前字符串,需要指定长度,从字符串的右侧开始填充

padStart

const str1 = 'Breaded Mushrooms';
console.log(str1.padEnd(25, '.'));
// output: "........Breaded Mushrooms"
// 方法会用一个字符串填充当前字符串,需要指定长度,从字符串的左侧开始填充

repeat

const str = 'abc';
str.repeat(3);
// 'abcabcabc';
// 方法返回一个重复多次的字符串

slice

slice 方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串
第一个参数:如果是负数,则会被视为string.length - 参数
第二个参数:可选的,如果省略,则一直会提取到字符串末尾,如果参数为负数,则会被视为string.length - 参数

const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(5, -2));
// > "uick brown fox jumps over the lazy do"

split

split 方法使用指定的分隔符将一个字符串分割成字符串数组

const str = 'The,quick,brown,fox jumps over the lazy dog.';
const words = str.split(",");
console.log(words);
// ["The", "quick", "brown", "fox jumps over the lazy dog."]

startsWith

startsWith 用来判断当前字符串是否以另外一个给定的字符串开头,返回true或者false

const str1 = 'Saturday night plans';
console.log(str1.startsWith('Sat'));
// true

substring

substring 方法返回一个字符串从开始索引到结束索引的子集

参数说明:

  • 如果 indexStart 等于 indexEndsubstring 返回一个空字符串。
  • 如果省略 indexEndsubstring 提取字符一直到字符串末尾。
  • 如果任一参数小于 0 或为 [NaN],则被当作 0。
  • 如果任一参数大于 stringName.length,则被当作 stringName.length
  • 如果 indexStart 大于 indexEnd,则 substring 的执行效果就像两个参数调换了一样。 ## substr

已经被废弃,尽可能不使用, 与上面的方法的区别是,substr的第二个参数是要截取字符串的长度,而不是结束index