JavaScript 常用字符串方法及使用技巧

123 阅读3分钟

------------------------前端小白,仅用作自我总结,有问题感谢指出------------------------

startsWith()

startsWith() 字符串以指定字符串开头,返回 true,否则返回 false,区分大小写。

语法:string.startsWith( searchValue, start)

let text = "Hello world, welcome to the universe.";
text.startsWith("Hello"); // true
text.startsWith("world", 6); // true
text.startsWith("world", 7); // false

endsWith()

endsWith() 字符串以规定字符串结尾,返回 true,否则返回 false,区分大小写。

语法:string.endsWith( searchvalue, length)

let text = "Hello world";
let result = text.endsWith("world"); // true

let text = "Hello world, welcome to the universe.";
// 检查字符串的前 11 个字符是否以 "world" 结尾:
text.endsWith("world", 11); // true

includes()

includes() 字符串包含指定的字符串,返回 true,否则返回 false,区分大小写。

语法:string.includes( searchvalue, start)

let text = "Hello world, welcome to the universe.";
let result = text.includes("world"); // true

// 从位置 12 开始:
let result = text.includes("world", 12); // false
let result = text.includes("universe", 12); // true

indexOf()

indexOf() 返回值在字符串中第一次出现的索引,未找到该值,返回 -1,区分大小写。

语法:string.indexOf( substring, start)

let text = "Hello world, welcome to the universe.";
let result = text.indexOf("welcome"); // 13

// 从位置 5 开始查找 "e" 的第一个匹配项:
text.indexOf("w", 5); // 6

search()

search() 将字符串与正则表达式匹配,如果搜索值为字符串,则转换为正则表达式,返回第一个匹配项的索引。未找到匹配项,则 search() 方法返回 -1search() 方法区分大小写。

语法:string.search( regexp)

let text = "Mr. Blue has a blue house";
let position = text.search("Blue"); // 4

match()

match() 将字符串与正则表达式进行匹配。搜索值为字符串,则转换为正则表达式。返回包含匹配项的新数组。未找到匹配项,则 match() 方法返回 null

语法:string.match( regexp)

let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain"); // ain
// 全局、不区分大小写的搜索:
text.match(/ain/gi); // ain,AIN,ain,ain

split()

split() 将字符串拆分为子字符串数组,返回新数组,不会更改原始字符串。如果 (" ") 用作分隔符,则字符串在单词之间进行拆分。

语法:string.split( separator, limit)

let text = "How are you doing today?";
const myArray = text.split(" "); // How,are,you,doing,today?

concat()

concat() 连接两个或多个字符串,返回新字符串,不会更改现有字符串。

语法:string.concat( string1, string2, ..., stringX)

let text1 = "Hello";
let text2 = "world!";
let result = text1.concat(" ", text2); // Hello world!

slice() 左闭右开

slice() 截取字符串的一部分,返回新字符串,不会更改原始字符串。

语法:string.slice( start, end)

let text = "Hello world!";
let result = text.slice(0, 5); // Hello

substring() 左闭右开

substring() 从字符串中提取两个索引之间的字符,返回子字符串。从头到尾(不包括)提取字符。方法不会更改原始字符串。如果 start 大于 end,则交换参数。

语法:string.substring( start, end)

let text = "Hello world!";
let result = text.substring(1, 4); // ell

substr()

substr() 方法提取字符串的一部分,从指定位置开始,并返回新字符串,不会更改原始字符串。

语法:string.substr( start, length)

let text = "Hello!";
let result = text.substr(1, 3);" // ell
let result = text.substr(2); // llo!

trim()

trim() 方法从字符串的两侧删除空格,并返回新字符串,不会更改原始字符串。

语法:string.trim()

let text = "       Hello World!        ";
let result = text.trim(); // Hello World!

trimStart()方法返回从开头删除空格的字符串。

trimEnd()返回从末尾删除空格的字符串。

replace()

replace() 在字符串中搜索值或正则表达式,返回已替换值的新字符串,不会更改原始字符串。

语法:string.replace( regexp, replacement)

let text = "Visit Microsoft!";
let result = text.replace("Microsoft", "restaurant"); // Visit restaurant!

// 全局的、不区分大小写的替换:
let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue/gi, "red"); // Mr red has a red house and a red car.

repeat()

repeat() 返回带有多个字符串副本的字符串,返回新字符串,不会更改原始字符串。

语法:string.repeat( count)

let text = "Hello world!";
let result = text.repeat(2); // Hello world!Hello world!

toUpperCase()

toUpperCase() 方法将字符串转换为大写字母,不会更改原始字符串。

语法:string.toUpperCase()

let text = "Hello World!";
let result = text.toUpperCase(); // HELLO WORLD!

toLowerCase()

toLowerCase() 方法将字符串转换为小写字母,不会更改原始字符串。

语法:string.toLowerCase()

let text = "Hello World!";
let result = text.toLowerCase(); // hello world!