目录
1、charAt( )
2、charCodeAt()
3、concat()
4、endsWith()
5、includes()
6、indexOf()
7、lastIndexOf()
8、padEnd()
9、padStart()
10、repeat()
11、replace()
12、slice()
13、split()
14、startsWith()
15、substring()
16、toLocaleLowerCase()
17、toLocaleUpperCase()
18、toLowerCase()
19、toUpperCase()
20、trim()
21、trimEnd()
22、trimStart()
charAt( ) 方法返回对应序号的符号
语法:charAt(index)
index 要返回的字符的索引,从零开始。
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.charAt(4))//q
charCodeAt()
方法返回一个整数,表示给定索引处的 UTF-16 码元,其值介于 0
和 65535
之间。
语法:
charCodeAt(index)
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.charCodeAt(6))//105
concat()
concat()
方法将字符串参数连接到调用的字符串,并返回一个新的字符串。
concat(str1)
concat(str1, str2)
concat(str1, str2, /* …, */ strN)
strN要连接到 str
的一个或多个字符串
const str1 = 'Hello';
const str2 = 'World';
console.log(str1.concat(' ', str2));//Hello World
console.log(str2.concat(' ', str1))//World Hello
endsWith()
endsWith()
方法用于判断一个字符串是否以指定字符串结尾,如果是则返回 true
,否则返回 false
。
endsWith(searchString)
endsWith(searchString, endPosition)
const str1 = 'Cats are the best!';
console.log(str1.endsWith('best!'));//true
includes()
includes()
方法执行区分大小写的搜索,以确定是否可以在一个字符串中找到另一个字符串,并根据情况返回 true
或 false
。
includes(searchString)
includes(searchString, position)
const sentence = 'The quick brown fox jumps over the lazy dog.';
const word = 'fox';
console.log(sentence.includes(word))//true
indexOf()
indexOf()
方法在字符串中搜索指定子字符串,并返回其第一次出现的位置索引。它可以接受一个可选的参数指定搜索的起始位置,如果找到了指定的子字符串,则返回的位置索引大于或等于指定的数字。
indexOf(searchString)
indexOf(searchString, position)
const paragraph = "I think Ruth's dog is cuter than your dog!";
const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);
console.log(indexOfFirst)//15
console.log(paragraph.indexOf('is'))//19
lastIndexOf()
lastIndexOf()
方法搜索该字符串并返回指定子字符串最后一次出现的索引。它可以接受一个可选的起始位置参数,并返回指定子字符串在小于或等于指定数字的索引中的最后一次出现的位置。
lastIndexOf(searchString)
lastIndexOf(searchString, position)
const paragraph = "I think Ruth's dog is cuter than your dog!";
const searchTerm = 'dog';
console.log(paragraph.lastIndexOf(searchTerm))//38
console.log(paragraph.lastIndexOf('than'))//28
padEnd()
padEnd()
方法会将当前字符串从末尾开始填充给定的字符串(如果需要会重复填充),直到达到给定的长度。填充是从当前字符串的末尾开始的。
padEnd(targetLength)
padEnd(targetLength, padString)
const str1 = 'Breaded Mushrooms';
console.log(str1.padEnd(25, '.'));//Breaded Mushrooms........
padStart()
padStart()
方法用另一个字符串填充当前字符串(如果需要会重复填充),直到达到给定的长度。填充是从当前字符串的开头开始的。
padStart(targetLength)
padStart(targetLength, padString)
const str1 = '5';
console.log(str1.padStart(2, '0'));//05
console.log(str1.padStart(6,'a'))//aaaaa5
repeat()
repeat()
方法构造并返回一个新字符串,其中包含指定数量的所调用的字符串副本,这些副本连接在一起。
repeat(count)
const mood = 'Happy! ';
console.log(`I feel ${mood.repeat(3)}`);//I feel Happy! Happy! Happy!
const speek='go '
console.log(`重要的事情说三遍${speek.repeat(3)}`)//重要的事情说三遍go go go
replace()
replace()
方法返回一个新字符串,其中一个、多个或所有匹配的 pattern
被替换为 replacement
。pattern
可以是字符串或 RegExp,replacement
可以是字符串或一个在每次匹配时调用的函数。如果 pattern
是字符串,则只会替换第一个匹配项。原始的字符串不会改变
replace(pattern, replacement)
参数:
pattern
可以是字符串或者一个带有 Symbol.replace 方法的对象,典型的例子就是正则表达式。任何没有
Symbol.replace
方法的值都会被强制转换为字符串。
replacement
可以是字符串或函数。
- 如果是字符串,它将替换由
pattern
匹配的子字符串。支持一些特殊的替换模式,请参阅下面的指定字符串作为替换项部分。- 如果是函数,将为每个匹配调用该函数,并将其返回值用作替换文本。下面的指定函数作为替换项部分描述了提供给此函数的参数。
const paragraph = "I think Ruth's dog is cuter than your dog!";
console.log(paragraph.replace("Ruth's", 'my'));//I think my dog is cuter than your dog!
slice()
slice()
方法提取字符串的一部分,并将其作为新字符串返回,而不修改原始字符串。
slice(indexStart)
slice(indexStart, indexEnd)
参数
indexStart
要返回的子字符串中包含的第一个字符的索引。
indexEnd 可选
要返回的子字符串中排除的第一个字符的索引。
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(31));//the lazy dog.
console.log(str.slice(4,10))//quick
split()
split()
方法接受一个模式,通过搜索模式将字符串分割成一个有序的子串列表,将这些子串放入一个数组,并返回该数组
split(separator)
split(separator, limit)
separator
描述每个分割应该发生在哪里的模式。可以是
undefined
,一个字符串,或者一个具有 Symbol.split 方法的对象——典型的例子是正则表达式。省略separator
或传递undefined
会导致split()
返回一个只包含所调用字符串数组。所有不是undefined
的值或不具有@@split
方法的对象都被强制转换为字符串。
limit 可选
一个非负整数,指定数组中包含的子字符串的数量限制。当提供此参数时,split 方法会在指定
separator
每次出现时分割该字符串,但在已经有limit
个元素时停止分割。任何剩余的文本都不会包含在数组中。
- 如果在达到极限之前就达到了字符串的末端,那么数组包含的条目可能少于
limit
。- 如果
limit
为0
,则返回[]
。
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' ');
console.log(words[3]);//fox
console.log(words[7])//lazy
console.log(words)
// ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
startsWith()
startsWith()
方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 true
或 false
。
startsWith(searchString)
startsWith(searchString, position)
searchString
要在该字符串开头搜索的子串。不能是正则表达式。所有不是正则表达式的值都会被强制转换为字符串,因此省略它或传递
undefined
将导致startsWith()
搜索字符串"undefined"
,这应该不是你想要的结果。position 可选
searchString
期望被找到的起始位置(即searchString
的第一个字符的索引)。默认为0
。
const str1 = 'Saturday night plans';
console.log(str1.startsWith('Sat'));//true
substring()
substring()
方法返回该字符串从起始索引到结束索引(不包括)的部分,如果未提供结束索引,则返回到字符串末尾的部分。
参数
indexStart
返回子字符串中第一个要包含的字符的索引。
indexEnd 可选
返回子字符串中第一个要排除的字符的索引。
返回值
包含给定字符串的指定部分的新字符串。
const str = 'Mozilla';
console.log(str.substring(1, 3));//oz
toLocaleLowerCase()
方法会根据特定区域设置的大小写映射规则,将字符串转换为小写形式并返回。
返回值:一个新的字符串,表示调用字符串根据特定区域设置的大小写映射规则转换得到的小写形式。
const dotted = 'İstanbul';
console.log(dotted.toLocaleLowerCase())//i̇stanbul
toLocaleUpperCase()
方法会根据特定区域设置的大小写映射规则,将字符串转换为大写形式并返回。
返回值:一个新的字符串,表示调用字符串根据特定区域设置的大小写映射规则转换得到的大写形式
const city = 'istanbul';
console.log(city.toLocaleUpperCase())//ISTANBUL.
toLowerCase()
方法将该字符串转换为小写形式。
返回值:一个新的字符串,表示转换为小写的调用字符串。
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toLowerCase());
//the quick brown fox jumps over the lazy dog.
toUpperCase()
方法将该字符串转换为大写形式。
toUpperCase()
方法返回将字符串转换为大写形式后的值。由于 JavaScript 中的字符串是不可变的,因此此方法不会影响字符串本身的值。
返回值:一个新的字符串,表示转换为大写的调用字符串。
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toUpperCase());
// THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
trim()
方法会从字符串的两端移除空白字符,并返回一个新的字符串,而不会修改原始字符串。
const greeting = ' Hello world! ';
console.log(greeting);
// Hello world!
console.log(greeting.trim());//Hello world!
trimEnd()
方法会从字符串的结尾移除空白字符,并返回一个新的字符串,而不会修改原始字符串。trimRight()
是该方法的别名。
const greeting = ' Hello world! ';
console.log(greeting);// Hello world!
console.log(greeting.trimEnd());// Hello world!
trimStart()
方法会从字符串的开头移除空白字符,并返回一个新的字符串,而不会修改原始字符串。trimLeft()
是该方法的别名。
const greeting = ' Hello world! ';
console.log(greeting);// Hello world!
console.log(greeting.trimStart()); //Hello world!