JavaScript 之 字符串方法的总结

一、创建字符串

1、内置构造函数创建:

let str = new String("hello");

2、字面量方式创建:

let str = "hello";

二、常用的字符串方法

1、查询当前下标所在的字符串内容 — charAt()

string.charAt()方法:
  功能:查询当前下标所在的字符串内容;
  参数:索引(下标);
  返回值:查询到的内容;
具体示例如下:

let str = 'hello world!';
console.log(str.charAt(6)); // w  包含空格所在下标,所以下标为6的字符串内容为 w                           

注:在字符串中空格也算一个字符,所以计算下标的时候也要把空格计算进去。

2、查询对应的字符串索引的内容的unicode编码 — charCodeAt()

string.charCodeAt()方法:
  功能:查询对应的字符串索引的内容的unicode编码 ;
  参数:索引;
  返回值:查询到的Unicode编码;
具体示例如下:

let str = 'hello world';
console.log(str.charCodeAt(6)); // 119

3、将unicode编码转换成相应的字符串内容 — fromCharCode()

String.fromCharCode()方法:
  功能:将unicode编码转换成相应的字符串内容;
  参数:Unicode编码;
  返回值:转换后的字符串;
具体示例如下:

let str = String.fromCharCode(119);
console.log(str); // w
console.log(typeof str); // string

4、查找子串在主串中的第一个出现时的下标 — indexOf()

string.indexOf()方法:
  功能:查找子串在主串中的第一个出现时的下标;
  参数:要查找的字符串(子串);
  返回值:下标,如果找不到,则返回下标;
具体示例如下:

let str = 'hello world'
console.log(str.indexOf('l')); // 2
console.log(str.indexOf('l', 6)); // 9

5、查找子串在主串中的最后一个出现时的下标 — lastIndexOf()

string.lastIndexOf()方法:
  功能:查找子串在主串中的最后一个出现时的下标;
  参数:要查找的字符串(子串);
  返回值:下标,如果找不到,则返回 -1;
具体示例如下:

let str = 'hello world';
console.log(str.lastIndexOf('l')); // 9 
console.log(str.lastIndexOf('l', 3)); // 3

6、将字符串中的英文转换成大写 — toUpperCase()

string.toUpperCase()方法:
  功能:将字符串中的英文转换成大写;
  参数:无;
  返回值:转换后的英文字符串,注:不改变原字符串;
具体示例如下:

let str = 'hello world';
console.log(str.toUpperCase());  // HELLO WORLD
console.log(str);  // hello world

let str1 = 'hello 123 world';
console.log(str1.toUpperCase()); // HELLO 123 WORLD
console.log(str1); // hello 123 world

let str2 = 'Hello World';
console.log(str2.toUpperCase()); // HELLO WORLD
console.log(str2); // Hello World

7、将字符串中的英文转换成小写 — toUpperCase()

string.toUpperCase()方法:
  功能:将字符串中的英文转换成小写;
  参数:无;
  返回值:转换后的英文字符串,注:不改变原字符串;
具体示例如下:


let str = 'HELLO WORLD';
console.log(str.toLowerCase());  // hello world
console.log(str);  // HELLO WORLD

let str1 = 'HELLO 123 WORLD';
console.log(str1.toLowerCase()); // hello 123 world
console.log(str1);  // HELLO 123 WORLD

let str2 = 'Hello World';
console.log(str2.toLowerCase()); // hello world
console.log(str2);  // Hello World

8、将字符串使用指定的分隔符连接其他的字符串成一个新的字符串 — concat()

string.concat()方法:
  功能:将字符串使用指定的分隔符连接成一个新的字符串;
  参数:参数1 指定的分割符;
     参数2 需要连接的字符串;
  返回值:连接好的字符串;
具体示例如下:

let str = 'abcde';
let str1 = '123';
console.log(str.concat(str1)); // abcde123
console.log(typeof str.concat(str1)); // string

console.log(str.concat('-', str1)); // abcde-123
console.log(typeof str.concat('-', str1)); // string

console.log(str.concat('', str1)); // abcde123
console.log(typeof str.concat('', str1)); // string

console.log(str.concat(' + ', str1)); // abcde + 123
console.log(typeof str.concat(' + ', str1)); // string

9、将字符串按照指定的分隔符(必须存在字符串当中的)拆分成数组 — split()

string.split()方法:
  功能:将字符串按照指定的分隔符(必须存在字符串当中的)拆分成数组;
  参数:指定的分隔符;
  返回值:分隔符为分隔点炸开形成的数组;
具体示例如下:

let str = 'name=张三';
console.log(str.split('=')); // [ 'name', '张三' ]
console.log(str); // name=张三

let str1 = '2020-6-6';
console.log(str1.split('-')); // [ '2020', '6', '6' ]
console.log(str1); // 2020-6-6

let str2 = 'a b c d e';
console.log(str2.split('')); // [ 'a', ' ', 'b', ' ', 'c', ' ', 'd', ' ', 'e' ]
console.log(str2); // a b c d e

let str3 = 'a b c d e';
console.log(str3.split(' ')); // [ 'a', 'b', 'c', 'd', 'e' ]
console.log(str3); // a b c d e

再次注意:在字符串中空格也算一个字符,所以计算的时候也要把空格计算进去;如示例中str2str3的区别。

10、截取字符串的方法 — substring()

string.substring()方法:
  功能:用来截取字符串的内容;
  参数:参数1 起始下标(不能为负数);
     参数2 结束下标(不含,可选参数,不能为负数);
  返回值:截取后的字符串;
具体示例如下:

let str = 'hello world';
console.log(str.substring(4)); // o world
console.log(str.substring(5)); //  world
console.log(str.substring(5).length); // 6
console.log(str.substring(5, 8)); //  wo
console.log(str.substring(5, 8).length); // 3

// 参数不能为负数,虽然不会报错,但实现的结果并不是我们想要的;
console.log(str.substring(-6)); // hello world  错误写法
console.log(str.substring(-6).length); // 11  错误写法
console.log(str.substring(-6, 8)); // hello wo  错误写法
console.log(str.substring(-6, 8).length); // 8  错误写法
console.log(str.substring(-6, -2)); // 为空  错误写法
console.log(str.substring(-6, -2).length); // 0  错误写法

注:在字符串中空格也算一个字符,所以截取的时候也要把空格计算进去。

11、截取字符串的方法 — substr()

string.substr()方法:
  功能:用来截取字符串的内容;
  参数:参数1 起始下标(可以为负数);
     参数2 截取长度(不含,可选参数(0和正整数,不能为负数));
  返回值:截取后的字符串;
具体示例如下:

let str = 'hello world';
console.log(str.substr(4)); // o world
console.log(str.substr(5));  //  world
console.log(str.substr(5).length); // 6
console.log(str.substr(5, 2)); //  w
console.log(str.substr(5, 2).length); // 2
console.log(str.substr(6, 2)); // wo
console.log(str.substr(6, 2).length); // 2

// 第二个参数不能为负数,虽然不会报错,但实现的结果并不是我们想要的;
console.log(str.substr(-6));//  world
console.log(str.substr(-6).length); // 6
console.log(str.substr(-6, 8)); //  world
console.log(str.substr(-6, 8).length); // 6
console.log(str.substr(-6, -2)); // 为空  错误写法
console.log(str.substr(-6, -2).length); // 0  错误写法

注:在字符串中空格也算一个字符,所以截取的时候也要把空格计算进去。

12、截取字符串的方法 — slice()

string.slice()方法:此方法的参数可以接收负数;
  功能:截取字符串的一部分内容;
  参数:参数1 起始下标(可以是负数,也可以是0和正整数,-1代表倒数第一个字符,以此类推)
     参数2 结束下标(不含,可选参数,可以是负数,也可以是0和正整数,-1代表倒数第一个字符,以此类推)
  
具体示例如下:

let str = 'hello world';
console.log(str.slice(4)); // o world
console.log(str.slice(5));  //  world
console.log(str.slice(5).length); // 6
console.log(str.slice(5, 8)); //  wo
console.log(str.slice(5, 8).length); // 3
console.log(str.slice(6, 8)); // wo
console.log(str.slice(6, 8).length); // 2

console.log(str.slice(-6)); //  world
console.log(str.slice(-6).length); // 6
console.log(str.slice(-6, 8)); //  wo
console.log(str.slice(-6, 8).length); // 3
console.log(str.slice(-6, -2)); //  wor
console.log(str.slice(-6, -2).length); // 4

注:在字符串中空格也算一个字符,所以截取的时候也要把空格计算进去。


截取字符串的三种方法substring()substr()slice()的异同点:
 相同点:1)功能都是用来截取字符串的;
     2)返回值都是截取后的字符串;
 不同点:参数的选取值不同;
     1)substring()方法的两个参数都不能为负数,只能是是 0 或者正整数;
     2)substr()方法的第一个参数可以是 0 或者正整数或者负数,而第二个参数不能为负数;
     3)slice()方法的两个参数可以是 0 和正整数,也可以是负数。

13、判断字符串是否包含指定子串 — includes()

string.includes()方法:
  功能:判断字符串是否包含指定子串;
  参数:子串;
  返回值:布尔值;
具体示例如下:

let str = 'hello world';
console.log(str.includes('llo')); // true
console.log(str.includes('ab')); // false

14、判断字符串是否以指定子串开头 — startsWith()

string.startsWith()方法:
  功能:判断字符串是否以指定子串开头;
  参数:子串;
  返回值:布尔值;
具体示例如下:

let str = 'hello world';
console.log(str.startsWith('he')); // true
console.log(str.startsWith('wo')); // false

15、判断字符串是否以指定子串结尾 — endsWith()

string.endsWith()方法:
  功能:判断字符串是否以指定子串结尾;
  参数:子串;
  返回值:布尔值;
具体示例如下:

let str = 'hello world';
console.log(str.endsWith('rld')); // true
console.log(str.endsWith('llo')); // false

16、将字符串重复 n 次形成新的字符串 — repeat()

string.repeat()方法:
  功能:将字符串重复 n 次形成新的字符串;
  参数:number(重复的次数,必须参数);
  返回值:重复后的字符串;
具体示例如下:

let str = 'hello world';
console.log(str.repeat()); // 为空
console.log(str.repeat(2)); // hello worldhello world

17、将字符串以指定字符开头定宽(长度) — padStart()

string.padStart()方法:
  功能:将字符串以指定字符开头定宽(长度);
  参数:参数1 定宽(指定字符串的length);
     参数2 指定的字符;
  返回值:转换后的字符串;
具体示例如下:

let str = 'hello world';
console.log(str.padStart(1)); // hello world
console.log(str.padStart(15)); //    hello world 
console.log(str.padStart(15).length); // 15
console.log(str.padStart(15, '0')); // 0000hello world
console.log(str.padStart(15, 'fff')); //ffffhello world

18、将字符串以指定字符结尾定宽(长度) — padEnd()

string.padEnd()方法:
  功能:将字符串以指定字符结尾定宽(长度);
  参数:参数1 定宽(指定字符串的length);
     参数2 指定的字符;
  返回值:转换后的字符串;
具体示例如下:

let str = 'hello world';
console.log(str.padEnd(1)); // hello world
console.log(str.padEnd(15)); // hello world 
console.log(str.padEnd(15).length); // 15
console.log(str.padEnd(15, '0')); // hello world0000
console.log(str.padEnd(15, 'fff')); // hello worldffff

19、替换字符串 — replace()

string.replace()方法:
  功能:替换字符串;
  参数:参数1 被替换的子串(可以是正则表达式);
     参数2 换上去的子串;
     参数 2 也可以是匿名函数,详情请看:JavaScript 之 replace() 方法的第二个参数为匿名函数时的理解(涉及正则).
  
具体示例如下:

let str = 'hello world';
console.log(str.replace('world', 'javascript')); // hello javascript
console.log(str.replace(/l/g, 'a')); // heaao worad

let str = "JavaScript";
let reg = /[A-Z]/g;		// g 为全局匹配的意思;
let str1 = str.replace(reg, function(){
    
    
    return arguments[0] + "(" + arguments[1] + ")";
});

console.log(str1); // J(0)avaS(4)cript





  以上就是本人对字符串方法的一些简单总结,如有不足之处,望大神们不吝指出,谢谢!

猜你喜欢

转载自blog.csdn.net/Zhuangvi/article/details/106590858