JS字符串常用方法
charAt()方法
返回给定索引位置的字符
let message = "abcde";
console.log(message.charAt(2)); // "c"
slice()方法、substr()方法、 substring()方法
slice()、substring()第一个参数表示子字符串开 始的位置,第二个参数表示子字符串结束的位置。含头不含尾
substr()第二个参数表示返回的子字符串数量
let stringValue = "hello world";
console.log(stringValue.slice(3)); // "lo world"
console.log(stringValue.substring(3)); // "lo world"
console.log(stringValue.substr(3)); // "lo world"
console.log(stringValue.slice(3, 7)); // "lo w"
console.log(stringValue.substring(3,7)); // "lo w"
console.log(stringValue.substr(3, 7)); // "lo worl"
slice()方法将所有负值参数都当成字 符串长度加上负参数值。
substr()将第二个负参数值转换为 0。
substring()方法会将所有负参数值都转换为 0
let stringValue = "hello world";
console.log(stringValue.slice(-3)); // "rld"
console.log(stringValue.substring(-3)); // "hello world"
console.log(stringValue.substr(-3)); // "rld"
console.log(stringValue.slice(3, -4)); // "lo w"
console.log(stringValue.substring(3, -4)); // "hel"
console.log(stringValue.substr(3, -4)); // "" (empty string)
indexOf()和 lastIndexOf()
indexOf()方法 从字符串开头开始查找子字符串,
lastIndexOf()方法从字符串末尾开始查找子字符串
let stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4
console.log(stringValue.lastIndexOf("o")); // 7
可以接收可选的第二个参数,表示开始搜索的位置
let stringValue = "hello world";
console.log(stringValue.indexOf("o", 6)); // 7
console.log(stringValue.lastIndexOf("o", 6)); // 4
查找所有目标字符串:
let stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
let positions = new Array();
let pos = stringValue.indexOf("e");
while(pos > -1) {
positions.push(pos);
pos = stringValue.indexOf("e", pos + 1);
}
console.log(positions); // [3,24,32,35,52]
trim()方法
这个方法会创建字符串的一个副本,删除前、 后所有空格符,再返回结果
let stringValue = " hello world ";
let trimmedStringValue = stringValue.trim();
console.log(stringValue); // " hello world "
console.log(trimmedStringValue); // "hello world"
trimLeft()和 trimRight()方法分别用于从字符串开始和末尾清理空格符
startsWith()方法 、endsWith()方法、includes()方法
startsWith()检查开始于索引 0 的匹配项,endsWith()检查开始于索 引(string.length - substring.length)的匹配项,而 includes()检查整个字符串
startsWith()和 includes()方法接收可选的第二个参数,表示开始搜索的位置。如果传入第二 个参数,则意味着这两个方法会从指定位置向着字符串末尾搜索
let message = "foobarbaz";
console.log(message.startsWith("foo")); // true
console.log(message.startsWith("foo", 1)); // false
console.log(message.endsWith("bar")); // false
console.log(message.endsWith("bar", 6)); // true
console.log(message.includes("bar")); // true
console.log(message.includes("bar", 4)); // false
repeat()方法
接收一个整数参数,表示要将字 符串复制多少次,然后返回拼接所有副本后的结果
let stringValue = "na ";
console.log(stringValue.repeat(16) + "batman");
// na na na na na na na na na na na na na na na na batman
padStart()和 padEnd()方法
padStart()和 padEnd()方法会复制字符串,如果小于指定长度,则在相应一边填充字符,直至 满足长度条件。这两个方法的第一个参数是长度,第二个参数是可选的填充字符串,默认为空格
let stringValue = "foo";
console.log(stringValue.padStart(6)); // " foo"
console.log(stringValue.padStart(9, ".")); // "......foo"
console.log(stringValue.padEnd(6)); // "foo "
console.log(stringValue.padEnd(9, ".")); // "foo......"
可选的第二个参数并不限于一个字符。如果提供了多个字符的字符串,则会将其拼接并截断以匹配 指定长度。此外,如果长度小于或等于字符串长度,则会返回原始字符串。
let stringValue = "foo";
console.log(stringValue.padStart(8, "bar")); // "barbafoo"
console.log(stringValue.padStart(2)); // "foo"
console.log(stringValue.padEnd(8, "bar")); // "foobarba"
console.log(stringValue.padEnd(2)); // "foo"
toLowerCase()、toLocaleLowerCase()、toUpperCase()、toLocaleUpperCase()
转换大小写
let stringValue = "hello world";
console.log(stringValue.toLocaleUpperCase()); // "HELLO WORLD"
console.log(stringValue.toUpperCase()); // "HELLO WORLD"
console.log(stringValue.toLocaleLowerCase()); // "hello world"
console.log(stringValue.toLowerCase()); // "hello world"
toLocaleLowerCase()和 toLocaleUpperCase()方法旨在基于 特定地区实现。在很多地区,地区特定的方法与通用的方法是一样的,如果不知道代码涉及什么语言,则最好使用地 区特定的转换方法。
replace()方法
这个方法接收两个参数,第一个 参数可以是一个 RegExp 对象或一个字符串(这个字符串不会转换为正则表达式),第二个参数可以是 一个字符串或一个函数。如果第一个参数是字符串,那么只会替换第一个子字符串。要想替换所有子字 符串,第一个参数必须为正则表达式并且带全局标记
let text = "cat, bat, sat, fat";
let result = text.replace("at", "ond");
console.log(result); // "cond, bat, sat, fat"
result = text.replace(/at/g, "ond");
console.log(result); // "cond, bond, sond, fond"