返回

字符串方法汇总(全面)

前端

一、字符串连接方法

1. concat() 方法

concat() 方法用于连接两个或多个字符串。其语法如下:

string.concat(string1, string2, ..., stringN)

其中,string 为要连接的第一个字符串,string1、string2、...、stringN 为要连接的其他字符串。

示例:

const str1 = 'Hello';
const str2 = 'World';
const str3 = str1.concat(str2);

console.log(str3); // 输出:HelloWorld

2. fromCharCode() 方法

fromCharCode() 方法用于将 Unicode 编码转换为字符串。其语法如下:

String.fromCharCode(codePoint1, codePoint2, ..., codePointN)

其中,codePoint1、codePoint2、...、codePointN 为要转换的 Unicode 编码。

示例:

const codePoint1 = 65; // A
const codePoint2 = 66; // B
const str = String.fromCharCode(codePoint1, codePoint2);

console.log(str); // 输出:AB

二、字符串搜索方法

1. includes() 方法

includes() 方法用于检查字符串中是否包含指定的子字符串。其语法如下:

string.includes(substring, start)

其中,string 为要搜索的字符串,substring 为要查找的子字符串,start 为开始搜索的位置(可选)。

示例:

const str = 'Hello World';
const result = str.includes('World');

console.log(result); // 输出:true

2. indexOf() 方法

indexOf() 方法用于返回指定子字符串在字符串中首次出现的位置。其语法如下:

string.indexOf(substring, start)

其中,string 为要搜索的字符串,substring 为要查找的子字符串,start 为开始搜索的位置(可选)。

示例:

const str = 'Hello World';
const result = str.indexOf('World');

console.log(result); // 输出:6

3. lastIndexOf() 方法

lastIndexOf() 方法用于返回指定子字符串在字符串中最后一次出现的位置。其语法如下:

string.lastIndexOf(substring, start)

其中,string 为要搜索的字符串,substring 为要查找的子字符串,start 为开始搜索的位置(可选)。

示例:

const str = 'Hello World';
const result = str.lastIndexOf('l');

console.log(result); // 输出:9

4. localeCompare() 方法

localeCompare() 方法用于比较两个字符串。其语法如下:

string.localeCompare(string2, locales, options)

其中,string 为要比较的第一个字符串,string2 为要比较的第二个字符串,locales 为要使用的语言环境(可选),options 为比较选项(可选)。

示例:

const str1 = 'Hello';
const str2 = 'World';
const result = str1.localeCompare(str2);

console.log(result); // 输出:-1

5. match() 方法

match() 方法用于匹配字符串中的正则表达式。其语法如下:

string.match(regexp)

其中,string 为要匹配的字符串,regexp 为要使用的正则表达式。

示例:

const str = 'Hello World';
const result = str.match(/World/);

console.log(result); // 输出:["World"]

6. replace() 方法

replace() 方法用于将字符串中的指定子字符串替换为另一个字符串。其语法如下:

string.replace(regexp, replacement)

其中,string 为要替换的字符串,regexp 为要查找的子字符串,replacement 为要替换的字符串。

示例:

const str = 'Hello World';
const result = str.replace('World', 'Universe');

console.log(result); // 输出:Hello Universe

7. search() 方法

search() 方法用于返回指定正则表达式在字符串中首次出现的位置。其语法如下:

string.search(regexp)

其中,string 为要搜索的字符串,regexp 为要使用的正则表达式。

示例:

const str = 'Hello World';
const result = str.search(/World/);

console.log(result); // 输出:6

三、字符串切割方法

1. slice() 方法

slice() 方法用于从字符串中提取子字符串。其语法如下:

string.slice(start, end)

其中,string 为要提取子字符串的字符串,start 为子字符串的起始位置,end 为子字符串的结束位置。

示例:

const str = 'Hello World';
const result = str.slice(6, 11);

console.log(result); // 输出:World

2. split() 方法

split() 方法用于将字符串分割为子字符串数组。其语法如下:

string.split(separator, limit)

其中,string 为要分割的字符串,separator 为分隔符,limit 为要返回的子字符串的最大数量(可选)。

示例:

const str = 'Hello World';
const result = str.split(' ');

console.log(result); // 输出:["Hello", "World"]

3. substr() 方法

substr() 方法用于从字符串中提取子字符串。其语法如下:

string.substr(start, length)

其中,string 为要提取子字符串的字符串,start 为子字符串的起始位置,length 为子字符串的长度。

示例:

const str = 'Hello World';
const result = str.substr(6, 5);

console.log(result); // 输出:World

4. substring() 方法

substring() 方法用于从字符串中提取子字符串。其语法如下:

string.substring(start, end)

其中,string 为要提取子字符串的字符串,start 为子字符串的起始位置,end 为子字符串的结束位置。

示例:

const str = 'Hello World';
const result = str.substring(6, 11);

console.log(result); // 输出:World

四、字符串转换方法

1. toLowerCase() 方法

toLowerCase() 方法用于将字符串转换为小写。其语法如下:

string.toLowerCase()

其中,string 为要转换为小写的字符串。

示例:

const str = 'Hello World';
const result = str.toLowerCase();

console.log(result); // 输出:hello world

2. toUpperCase() 方法

toUpperCase() 方法用于将字符串转换为大写。其语法如下:

string.toUpperCase()

其中,string 为要转换为大写的字符串。

示例:

const str = 'Hello World';
const result = str.toUpperCase();

console.log(result); // 输出:HELLO WORLD

3. toString() 方法

toString() 方法用于将字符串转换为字符串。其语法如下:

string.toString()

其中,string 为要转换为字符串的字符串。

示例:

const str = 'Hello World';
const result = str.toString();

console.log(result); // 输出:Hello World

五、字符串修剪方法

1. trim() 方法

trim() 方法用于修剪字符串两端的空格。其语法如下:

string.trim()

其中,string 为要修剪的字符串。

示例:

const str = ' Hello World ';
const result = str.trim();

console.log(result); // 输出:Hello World

2. trimEnd() 方法

trimEnd() 方法用于修剪字符串末尾的空格。其语法如下:

string.trimEnd()