方法分类汇总索引
获取字符串信息方法 :
字符串查找方法 :
charAt():返回指定索引位置的字符
charCodeAt():返回指定索引位置字符的 Unicode 编码
indexOf():查找子字符串首次出现的位置
lastIndexOf():查找子字符串最后出现的位置
includes():判断是否包含指定子字符串
startsWith():判断是否以指定子字符串开头
endsWith():判断是否以指定子字符串结尾
字符串截取/提取方法 :
slice():提取字符串的一部分并返回新字符串
substring():提取两个指定索引之间的字符
substr():从指定位置提取指定长度的子字符串(不推荐使用)
字符串转换方法 :
toLowerCase():将字符串转换为小写
toUpperCase():将字符串转换为大写
toString():返回字符串本身
valueOf():返回字符串的原始值
字符串替换/分割方法 :
replace():查找匹配子字符串并替换为新内容
split():将字符串分割成字符串数组
字符串修剪方法 :
trim():去除字符串两端的空白字符
trimStart()/trimLeft():去除字符串开头的空白字符
trimEnd()/trimRight():去除字符串结尾的空白字符
字符串重复方法 :
字符串连接方法 :
字符串填充方法 :
padStart():在字符串开头填充字符至指定长度
padEnd():在字符串结尾填充字符至指定长度
其他字符串方法 :
match():查找匹配正则表达式的结果
matchAll():返回所有匹配正则表达式的迭代器
search():查找与正则表达式匹配的子字符串位置
localeCompare():考虑区域设置比较两个字符串
以下为 JavaScript 中常用的字符串方法详解,这些方法都不会改变原字符串,而是返回一个新的字符串或其他类型的值。
一、获取字符串信息属性 1. length
作用 :返回字符串的长度(字符数量)
语法 :str.length
参数 :无参数
返回值 :数字,表示字符串的长度
是否改变原字符串 :否
示例 :
1 2 3 4 5 const str = "Hello World" ;const len = str.length ;console .log (len);
二、字符串查找方法 1. charAt()
作用 :返回指定索引位置的字符
语法 :str.charAt(index)
参数 :index - 必需,表示字符在字符串中的索引(从 0 开始)
返回值 :字符串,指定索引处的字符;如果索引超出范围,则返回空字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 const str = "JavaScript" ;const char = str.charAt (4 );console .log (char); const invalidChar = str.charAt (20 );console .log (invalidChar);
2. charCodeAt()
作用 :返回指定索引位置字符的 Unicode 编码
语法 :str.charCodeAt(index)
参数 :index - 必需,字符在字符串中的索引(从 0 开始)
返回值 :数字,表示指定索引处字符的 Unicode 编码;如果索引超出范围,则返回 NaN
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 const str = "A" ;const code = str.charCodeAt (0 );console .log (code); const invalidCode = str.charCodeAt (1 );console .log (invalidCode);
3. indexOf()
作用 :查找某个子字符串在当前字符串中首次出现的位置
语法 :str.indexOf(searchValue[, fromIndex])
参数 :
searchValue - 必需,要查找的子字符串
fromIndex - 可选,开始查找的索引位置,默认为 0
返回值 :数字,子字符串首次出现的索引;如果没有找到,则返回-1
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 const str = "Hello, Hello World" ;const firstPos = str.indexOf ("Hello" );console .log (firstPos); const posFrom5 = str.indexOf ("Hello" , 5 );console .log (posFrom5); const notFound = str.indexOf ("JavaScript" );console .log (notFound);
4. lastIndexOf()
作用 :查找某个子字符串在当前字符串中最后出现的位置
语法 :str.lastIndexOf(searchValue[, fromIndex])
参数 :
searchValue - 必需,要查找的子字符串
fromIndex - 可选,开始查找的索引位置,默认为字符串的长度-1
返回值 :数字,子字符串最后出现的索引;如果没有找到,则返回-1
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 const str = "Hello, Hello World" ;const lastPos = str.lastIndexOf ("Hello" );console .log (lastPos); const posBefore6 = str.lastIndexOf ("Hello" , 6 );console .log (posBefore6);
5. includes()
作用 :判断当前字符串是否包含指定的子字符串
语法 :str.includes(searchValue[, fromIndex])
参数 :
searchValue - 必需,要查找的子字符串
fromIndex - 可选,开始查找的索引位置,默认为 0
返回值 :布尔值,如果包含则返回 true,否则返回 false
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 const str = "Hello World" ;const hasWorld = str.includes ("World" );console .log (hasWorld); const hasWorldFrom6 = str.includes ("World" , 6 );console .log (hasWorldFrom6); const hasJava = str.includes ("Java" );console .log (hasJava);
6. startsWith()
作用 :判断当前字符串是否以指定的子字符串开头
语法 :str.startsWith(searchValue[, position])
参数 :
searchValue - 必需,要查找的子字符串
position - 可选,开始查找的位置,默认为 0
返回值 :布尔值,如果以指定子字符串开头则返回 true,否则返回 false
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 const str = "Hello World" ;const startsWithHello = str.startsWith ("Hello" );console .log (startsWithHello); const startsWithW = str.startsWith ("W" , 6 );console .log (startsWithW); const startsWithWorld = str.startsWith ("World" );console .log (startsWithWorld);
7. endsWith()
作用 :判断当前字符串是否以指定的子字符串结尾
语法 :str.endsWith(searchValue[, length])
参数 :
searchValue - 必需,要查找的子字符串
length - 可选,作为字符串长度使用的值,默认为字符串的实际长度
返回值 :布尔值,如果以指定子字符串结尾则返回 true,否则返回 false
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 const str = "Hello World" ;const endsWithWorld = str.endsWith ("World" );console .log (endsWithWorld); const endsWithHello = str.endsWith ("Hello" , 5 );console .log (endsWithHello); const endsWithHelloFull = str.endsWith ("Hello" );console .log (endsWithHelloFull);
三、字符串截取/提取方法 1. slice()
作用 :提取字符串的一部分,并返回一个新的字符串
语法 :str.slice(startIndex[, endIndex])
参数 :
startIndex - 必需,提取的起始索引(包含)
endIndex - 可选,提取的结束索引(不包含),默认为字符串的长度
返回值 :字符串,提取的子字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 const str = "Hello World" ;const world = str.slice (6 );console .log (world); const hello = str.slice (0 , 5 );console .log (hello); const last3 = str.slice (-3 );console .log (last3);
2. substring()
作用 :提取字符串中介于两个指定索引之间的字符
语法 :str.substring(indexStart[, indexEnd])
参数 :
indexStart - 必需,提取的起始索引(包含)
indexEnd - 可选,提取的结束索引(不包含),默认为字符串的长度
返回值 :字符串,提取的子字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 const str = "Hello World" ;const hello = str.substring (0 , 5 );console .log (hello); const swapped = str.substring (5 , 0 );console .log (swapped); const negative = str.substring (-3 , 5 );console .log (negative);
3. substr()
作用 :从指定位置开始提取指定长度的子字符串(注意:该方法已不推荐使用)
语法 :str.substr(start[, length])
参数 :
start - 必需,提取的起始索引
length - 可选,要提取的字符数,默认为到字符串的结尾
返回值 :字符串,提取的子字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 const str = "Hello World" ;const world = str.substr (6 , 5 );console .log (world); const hello = str.substr (0 , 5 );console .log (hello); const last3 = str.substr (-3 );console .log (last3);
四、字符串转换方法 1. toLowerCase()
作用 :将字符串转换为小写
语法 :str.toLowerCase()
参数 :无参数
返回值 :字符串,转换为小写的新字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 const str = "Hello World" ;const lowerStr = str.toLowerCase ();console .log (lowerStr); console .log (str);
2. toUpperCase()
作用 :将字符串转换为大写
语法 :str.toUpperCase()
参数 :无参数
返回值 :字符串,转换为大写的新字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 const str = "Hello World" ;const upperStr = str.toUpperCase ();console .log (upperStr); console .log (str);
3. toString()
作用 :返回字符串本身
语法 :str.toString()
参数 :无参数
返回值 :字符串,与原字符串相同
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 const str = "Hello" ;const sameStr = str.toString ();console .log (sameStr); const strObj = new String ("World" );console .log (typeof strObj); const strVal = strObj.toString ();console .log (typeof strVal);
4. valueOf()
作用 :返回字符串的原始值
语法 :str.valueOf()
参数 :无参数
返回值 :字符串,字符串的原始值
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 const str = "Hello" ;console .log (str.valueOf ()); const strObj = new String ("World" );console .log (strObj.valueOf ()); console .log (typeof strObj.valueOf ());
五、字符串替换/分割方法 1. replace()
作用 :在字符串中查找匹配的子字符串,并替换为新的子字符串
语法 :str.replace(regexp|substr, newSubstr|function)
参数 :
第一个参数:可以是字符串或正则表达式,表示要查找的内容
第二个参数:可以是字符串或函数,表示替换的内容
返回值 :字符串,替换后的新字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 const str = "Hello World, Hello JavaScript" ;const replacedOnce = str.replace ("Hello" , "Hi" );console .log (replacedOnce); const replacedAll = str.replace (/Hello/g , "Hi" );console .log (replacedAll); const replacedWithFunc = str.replace (/Hello/g , (match ) => { return match.toUpperCase (); }); console .log (replacedWithFunc);
2. split()
作用 :将字符串分割成字符串数组
语法 :str.split([separator[, limit]])
参数 :
separator - 可选,字符串或正则表达式,用于指定分割的位置
limit - 可选,数字,限制返回的数组长度
返回值 :数组,分割后的字符串数组
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 13 const str = "apple,banana,orange,grape" ;const fruits = str.split ("," );console .log (fruits); const limitedFruits = str.split ("," , 2 );console .log (limitedFruits); const chars = "hello" .split ("" );console .log (chars);
六、字符串修剪方法 1. trim()
作用 :去除字符串两端的空白字符(包括空格、制表符、换行符等)
语法 :str.trim()
参数 :无参数
返回值 :字符串,去除两端空白后的新字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 const str = " Hello World " ;const trimmed = str.trim ();console .log (trimmed); console .log (trimmed.length );
2. trimStart() / trimLeft()
作用 :去除字符串开头(左侧)的空白字符
语法 :str.trimStart() 或 str.trimLeft()
参数 :无参数
返回值 :字符串,去除开头空白后的新字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 const str = " Hello World " ;const trimmedStart = str.trimStart ();console .log (trimmedStart); console .log (trimmedStart.length ); const trimmedLeft = str.trimLeft ();console .log (trimmedLeft);
3. trimEnd() / trimRight()
作用 :去除字符串结尾(右侧)的空白字符
语法 :str.trimEnd() 或 str.trimRight()
参数 :无参数
返回值 :字符串,去除结尾空白后的新字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 const str = " Hello World " ;const trimmedEnd = str.trimEnd ();console .log (trimmedEnd); console .log (trimmedEnd.length ); const trimmedRight = str.trimRight ();console .log (trimmedRight);
七、字符串重复方法 1. repeat()
作用 :将字符串重复指定的次数
语法 :str.repeat(count)
参数 :count - 必需,数字,表示重复的次数(0 到正无穷大之间的整数)
返回值 :字符串,重复指定次数后的新字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 const str = "Hi" ;const repeated = str.repeat (3 );console .log (repeated); const empty = str.repeat (0 );console .log (empty); try { str.repeat (-1 ); } catch (e) { console .log (e); }
八、字符串连接方法 1. concat()
作用 :连接两个或多个字符串
语法 :str.concat(str1[, str2[, ...[, strN]]])
参数 :str1, str2, ..., strN - 要连接的字符串
返回值 :字符串,连接后的新字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 const str1 = "Hello" ;const str2 = " " ;const str3 = "World" ;const result = str1.concat (str2, str3, "!" );console .log (result); const sameResult = str1 + str2 + str3 + "!" ;console .log (sameResult);
九、字符串填充方法 1. padStart()
作用 :在字符串的开头填充指定的字符,直到达到指定的长度
语法 :str.padStart(targetLength[, padString])
参数 :
targetLength - 必需,数字,目标字符串的长度
padString - 可选,用于填充的字符串,默认为空格
返回值 :字符串,填充后的新字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 const str = "5" ;const padded = str.padStart (3 );console .log (padded); const zeroPadded = str.padStart (5 , "0" );console .log (zeroPadded); const shorter = str.padStart (1 );console .log (shorter);
2. padEnd()
作用 :在字符串的结尾填充指定的字符,直到达到指定的长度
语法 :str.padEnd(targetLength[, padString])
参数 :
targetLength - 必需,数字,目标字符串的长度
padString - 可选,用于填充的字符串,默认为空格
返回值 :字符串,填充后的新字符串
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 const str = "5" ;const padded = str.padEnd (3 );console .log (padded); const zeroPadded = str.padEnd (5 , "0" );console .log (zeroPadded); const multiChar = "Hi" .padEnd (7 , "abc" );console .log (multiChar);
十、其他字符串方法 1. match()
作用 :在字符串中查找匹配正则表达式的结果
语法 :str.match(regexp)
参数 :regexp - 必需,正则表达式对象
返回值 :数组,包含匹配结果;如果没有找到匹配,则返回 null
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 const str = "The quick brown fox jumps over the lazy dog" ;const matches = str.match (/o\w+/g );console .log (matches); const firstMatch = str.match (/q\w+/ );console .log (firstMatch); const noMatch = str.match (/z\w+/ );console .log (noMatch);
2. matchAll()
作用 :返回一个包含所有匹配正则表达式的结果及分组捕获的迭代器
语法 :str.matchAll(regexp)
参数 :regexp - 必需,正则表达式对象(必须包含全局标志 g)
返回值 :迭代器,包含所有匹配结果
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 13 const str = "Hello 123, Hello 456" ;const regex = /Hello (\d+)/g ;const matches = str.matchAll (regex);const results = Array .from (matches);console .log (results);
3. search()
作用 :查找与正则表达式相匹配的子字符串的位置
语法 :str.search(regexp)
参数 :regexp - 必需,正则表达式对象
返回值 :数字,第一个匹配项的索引;如果没有找到匹配,则返回-1
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 12 const str = "Hello World" ;const upperPos = str.search (/[A-Z]/ );console .log (upperPos); const worldPos = str.search (/World/ );console .log (worldPos); const noPos = str.search (/Java/ );console .log (noPos);
4. localeCompare()
作用 :比较两个字符串,考虑当前区域设置
语法 :str.localeCompare(compareString[, locales[, options]])
参数 :
compareString - 必需,要比较的字符串
locales - 可选,指定区域设置
options - 可选,配置比较选项
返回值 :数字,表示比较结果(-1:当前字符串在前;0:相等;1:当前字符串在后)
是否改变原字符串 :否
示例 :
1 2 3 4 5 6 7 8 9 10 11 const str1 = "apple" ;const str2 = "banana" ;console .log (str1.localeCompare (str2)); console .log (str2.localeCompare (str1)); console .log (str1.localeCompare ("apple" )); console .log ("ä" .localeCompare ("z" , "de" )); console .log ("ä" .localeCompare ("z" , "sv" ));