属性
var x = "Mozilla";x.length
方法
charAt
str.charAt(index)var str = "abc"str[3]; //undefinedstr.charAt(3) // ""
字符串是一个类似数组的对象,有length属性,可以通过数值键(0,1)取值;
str[i] 与 str.charAt(i)本质上没有区别,只是str.charAt(i)更标准而已
字符串处理
| 方法 | 描述 | 
|---|---|
| concat() | 连接两个或更多字符串,并返回新的字符串。 | 
| split() | 把字符串分割为字符串数组。 | 
| trim() | 去除字符串两边的空白。 | 
| repeat() | 复制字符串指定次数,并将它们连接在一起返回。 | 
| slice() | 提取字符串的片断,并在新的字符串中返回被提取的部分。 | 
| substring() | 提取字符串中两个指定的索引号之间的字符。 | 
| substr() | 从起始索引号提取字符串中指定数目的字符。 | 
| toLowerCase() | 把字符串转换为小写。 | 
| toUpperCase() | 把字符串转换为大写。 | 
concat
强烈建议使用赋值操作符(+, +=)代替 concat 方法
var s1 = 'abc';var s2 = 'def';s1.concat(s2) // "abcdef"
split
const str = 'The quick brown fox jumps over the lazy dog.';const words = str.split(' ');console.log(words[3]);// expected output: "fox"
trim
const greeting = ' Hello world! ';console.log(greeting);// expected output: " Hello world! ";console.log(greeting.trim());// expected output: "Hello world!";
repeat
"abc".repeat(0) // """abc".repeat(1) // "abc""abc".repeat(2) // "abcabc"
slice
str.slice(beginIndex, endIndex)var str1 = 'The morning is upon us.', // str1 的长度 length 是 23。str2 = str1.slice(1, 8),str3 = str1.slice(4, -2),str4 = str1.slice(12),str5 = str1.slice(30);console.log(str2); // 输出:he mornconsole.log(str3); // 输出:morning is upon uconsole.log(str4); // 输出:is upon us.console.log(str5); // 输出:""
substring
var anyString = "Mozilla";// 输出 "Moz"console.log(anyString.substring(0,3));
padStart/padEnd
'abc'.padStart(10); // " abc"'abc'.padStart(10, "foo"); // "foofoofabc"'abc'.padStart(6,"123465"); // "123abc"'abc'.padStart(8, "0"); // "00000abc"
'abc'.padEnd(10); // "abc "'abc'.padEnd(10, "foo"); // "abcfoofoof"
字符串判断
| 方法 | 描述 | 
|---|---|
| includes() | 查找字符串中是否包含指定的子字符串。 | 
| startsWith() | 查看字符串是否以指定的子字符串开头。 | 
| endsWith() | 判断当前字符串是否是以指定的子字符串结尾的(区分大小写)。 | 
| indexOf() | 返回某个指定的字符串值在字符串中首次出现的位置。 | 
| lastIndexOf() | 从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置。 | 
includes
这个方法可以帮你判断一个字符串是否包含另外一个字符串。
str.includes(searchString, position)'Blue Whale'.includes('blue'); // returns false 区分大小写
startsWith/endsWith
endsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。
str.endsWith(searchString,length)var str = "To be, or not to be, that is the question.";alert( str.endsWith("question.") ); // truealert( str.endsWith("to be") ); // falsealert( str.endsWith("to be", 19) ); // true
indexOf/lastIndexOf
indexOf() 方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。
str.indexOf(searchValue,fromIndex)
该方法将从尾到头地检索字符串 str,看它是否含有子串 searchValue。开始检索的位置在字符串的 fromIndex 处或字符串的结尾(没有指定 fromIndex 时)。如果找到一个 searchValue,则返回 searchValue 的第一个字符在 str 中的位置。str中的字符位置是从 0 开始的。
正则
| 方法 | 描述 | 
|---|---|
| match() | 查找找到一个或多个正则表达式的匹配。 | 
| replace() | 在字符串中查找匹配的子串,并替换与正则表达式匹配的子串。 | 
| replaceAll() | 在字符串中查找匹配的子串,并替换与正则表达式匹配的所有子串。 | 
| search() | 查找与正则表达式相匹配的值。 | 
match
match(regExp) 方法检索返回一个字符串匹配正则表达式的结果。
const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';const regex = /[A-Z]/g;const found = paragraph.match(regex);
matchAll(regExp) 方法返回一个包含所有匹配正则表达式的结果及分组捕获组的迭代器。
regExp必须是设置了全局模式g的形式,否则会抛出异常TypeError。
replace
str.replace(regexp|substr, newSubStr|function)console.log(p.replace('dog', 'monkey'));// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"const regex = /Dog/i;console.log(p.replace(regex, 'ferret'));// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
search
执行正则表达式和 String 对象之间的一个搜索匹配。
var str = "hey JudE";var re = /[A-Z]/g;var re2 = /[.]/g;console.log(str.search(re)); // returns 4, which is the index of the first capital letter "J"console.log(str.search(re2)); // returns -1 cannot find '.' dot punctuation
