js基础-字符串方法

106 阅读4分钟

一、字符串方法

JavaScript 提供了多种字符串方法,用于操作和处理字符串。

常用字符串方法

  1. toLowerCase():将字符串转换为小写。
  2. toUpperCase():将字符串转换为大写。
  3. charAt(index):返回指定索引位置的字符。
  4. indexOf(searchValue):返回指定字符串首次出现的位置,如果未找到则返回 -1。
  5. lastIndexOf(searchValue):返回指定字符串最后一次出现的位置。
  6. substring(start, end):提取字符串中介于两个指定索引之间的字符。
  7. slice(start, end):提取字符串的一部分并返回新的字符串,end 参数是非包含的。
  8. trim():去除字符串两端的空格。
  9. includes(searchValue):判断字符串是否包含指定的值,返回布尔值。
  10. replace(searchValue, newValue):替换匹配到的第一个子串并返回新的字符串。
  11. split(separator):将字符串分割成数组,分隔符可以是一个字符串或正则表达式。
  12. concat():连接两个或多个字符串并返回新的字符串。
1. toLowerCase():将字符串转换为小写字母。
let text = "HELLO WORLD";
let lowerText = text.toLowerCase();
console.log(lowerText); // 输出: hello world
2. toUpperCase():将字符串转换为大写字母。
let text = "hello world";
let upperText = text.toUpperCase();
console.log(upperText); // 输出: HELLO WORLD
3. charAt(index):返回指定索引位置的字符。
let text = "JavaScript";
let char = text.charAt(0);
console.log(char); // 输出: J
4. indexOf(searchValue):返回指定字符串首次出现的位置,如果未找到则返回 -1。
let text = "Hello, World!";
let index = text.indexOf("World");
console.log(index); // 输出: 7
5. lastIndexOf(searchValue):返回指定字符串最后一次出现的位置。
let text = "Hello, World! Hello!";
let lastIndex = text.lastIndexOf("Hello");
console.log(lastIndex); // 输出: 13
6. substring(start, end):提取字符串中介于两个指定索引之间的字符。
let text = "Hello, World!";
let subText = text.substring(7, 12);
console.log(subText); // 输出: World
7. slice(start, end):提取字符串的一部分并返回新的字符串,end参数是非包含的。
let text = "Hello, World!";
let slicedText = text.slice(0, 5);
console.log(slicedText); // 输出: Hello
8. trim():去除字符串两端的空格。
let text = "   Hello, World!   ";
let trimmedText = text.trim();
console.log(trimmedText); // 输出: Hello, World!
9. includes(searchValue): 判断字符串是否包含指定的值,返回布尔值。
let text = "Hello, World!";
let containsHello = text.includes("Hello");
console.log(containsHello); // 输出: true
10. replace(searchValue, newValue):替换匹配到的第一个子串并返回新的字符串。
let text = "Hello, World!";
let newText = text.replace("World", "JavaScript");
console.log(newText); // 输出: Hello, JavaScript!
11. split(separator):将字符串分割成数组,分隔符可以是一个字符串或正则表达式。
let text = "Apple,Banana,Kiwifruit";
let fruits = text.split(",");
console.log(fruits); // 输出: [ 'Apple', 'Banana', 'Kiwifruit' ]
12. concat():连接两个或多个字符串并返回新的字符串。
let str1 = "Hello";
let str2 = "World";
let combined = str1.concat(", ", str2);
console.log(combined); // 输出: Hello, World

其余字符串方法

  1. at(index) :返回指定索引位置的字符,支持负索引。
  2. charCodeAt(index) :返回指定索引位置字符的Unicode编码。
  3. codePointAt(index) :返回指定索引位置字符的代码点值。
  4. endsWith(searchString) :判断字符串是否以指定的字符串结尾,返回布尔值。
  5. startsWith(searchString) :判断字符串是否以指定的字符串开头,返回布尔值。
  6. replaceAll(searchValue, newValue) :替换所有匹配到的子串并返回新的字符串。
  7. repeat(count) :返回一个新字符串,该字符串是原字符串重复指定次数的结果。
  8. padStart(targetLength, padString) :在原字符串前填充指定的字符,直到达到目标长度。
  9. padEnd(targetLength, padString) :在原字符串后填充指定的字符,直到达到目标长度。
  10. toLocaleLowerCase() :根据主机的区域设置将字符串转换为小写字母。
  11. toLocaleUpperCase() :根据主机的区域设置将字符串转换为大写字母。
  12. valueOf() :返回字符串对象的原始值。
1. at(index): 指定索引位置的字符,支持负索引。
let str = "Hello";
console.log(str.at(1));    // 输出: e
console.log(str.at(-1));   // 输出: o
2. charCodeAt(index): 指定索引位置字符的Unicode编码。
let text = "ABC";
console.log(text.charCodeAt(0)); // 输出: 65
3. codePointAt(index): 指定索引位置字符的代码点值。
let str = "𠮷"; // 一个Unicode字符
console.log(str.codePointAt(0)); // 输出: 134071
4. endsWith(searchString):判断字符串是否以指定的字符串结尾,返回布尔值。
let str = "Hello, World!";
console.log(str.endsWith("World!")); // 输出: true
5. startsWith(searchString):判断字符串是否以指定的字符串开头,返回布尔值。
let str = "Hello, World!";
console.log(str.startsWith("Hello")); // 输出: true
6. replaceAll(searchValue, newValue):替换所有匹配到的子串并返回新的字符串。
let str = "Hello, World! Hello!";
let newStr = str.replaceAll("Hello", "Hi");
console.log(newStr); // 输出: Hi, World! Hi!
7. repeat(count): 一个新字符串,该字符串是原字符串重复指定次数的结果。
let str = "Ha!";
console.log(str.repeat(3)); // 输出: Ha!Ha!Ha!
8. padStart(targetLength, padString):在原字符串前填充指定的字符,直到达到目标长度。
let str = "5";
console.log(str.padStart(2, '0')); // 输出: 05
9. padEnd(targetLength, padString):在原字符串后填充指定的字符,直到达到目标长度。
let str = "5";
console.log(str.padEnd(2, '0')); // 输出: 50
10. toLocaleLowerCase():根据主机的区域设置将字符串转换为小写字母。
let str = "HELLO WORLD";
console.log(str.toLocaleLowerCase()); // 输出: hello world
11. toLocaleUpperCase():根据主机的区域设置将字符串转换为大写字母。
let str = "hello world";
console.log(str.toLocaleUpperCase()); // 输出: HELLO WORLD
12. valueOf(): 字符串对象的原始值。
let str = new String("Hello");
console.log(str.valueOf()); // 输出: Hello