文章目录 charAt(index): 返回指定索引处的字符 charCodeAt(index): 返回指定索引处字符的Unicode编码 concat(str1, str2, ..., strN): 连接两个或多个字符串,并返回新的字符串 includes(searchString, position): 判断一个字符串是否包含在另一个字符串中,根据情况返回true或false indexOf(searchValue, fromIndex): 返回在字符串中首次找到指定值的索引,如果不存在则返回-1 lastIndexOf(searchValue, fromIndex): 返回指定值在字符串中最后一次出现的位置,如果不存在则返回-1 match(regexp): 使用正则表达式与字符串相匹配 replace(searchValue, newValue): 在字符串中查找匹配项并替换为新的值 search(regexp): 使用正则表达式搜索字符串,并返回匹配项的索引 slice(beginIndex, endIndex): 提取字符串的片段,并在新的字符串中返回被提取的部分 split(separator, limit): 通过将字符串分割成子字符串数组,来将一个字符串分割成字符串数组 substring(start, end): 提取字符串中两个指定的索引号之间的字符 toLowerCase()和toUpperCase(): 将字符串转换为小写/大写 toLocaleLowerCase() 和 toLocaleUpperCase():根据本地主机的语言环境将字符串转换为小写或大写。考虑了本地化的特殊字符 trim(): 去除字符串两端的空白字符 startsWith(searchString, position) 和endsWith(searchString, length):: 去除字符串两端的空白字符 substr(start, length): 从起始索引号提取指定长度的子字符串 repeat(count): 返回一个新字符串,该字符串包含将原字符串重复指定的次数后的结果 localeCompare(that, locales, options): 比较两个字符串,并返回用于表示两个字符串在排序顺序中的相对位置的数字 padStart(targetLength, padString) 和 padEnd(targetLength, padString): 在当前字符串的开始或末尾填充指定的字符串,直到达到目标长度。如果不指定填充字符串,则默认使用空格填充
charAt(index): 返回指定索引处的字符
let str = "Hello" ;
console. log ( str. charAt ( 1 ) ) ;
charCodeAt(index): 返回指定索引处字符的Unicode编码
let str = "Hello" ;
console. log ( str. charCodeAt ( 1 ) ) ;
concat(str1, str2, …, strN): 连接两个或多个字符串,并返回新的字符串
let str1 = "Hello " ;
let str2 = "World!" ;
console. log ( str1. concat ( str2) ) ;
includes(searchString, position): 判断一个字符串是否包含在另一个字符串中,根据情况返回true或false
let str = "Hello World!" ;
console. log ( str. includes ( "World" ) ) ;
indexOf(searchValue, fromIndex): 返回在字符串中首次找到指定值的索引,如果不存在则返回-1
let str = "Hello World!" ;
console. log ( str. indexOf ( "World" ) ) ;
lastIndexOf(searchValue, fromIndex): 返回指定值在字符串中最后一次出现的位置,如果不存在则返回-1
let str = "Hello World! Hello!" ;
console. log ( str. lastIndexOf ( "Hello" ) ) ;
match(regexp): 使用正则表达式与字符串相匹配
let str = "The quick brown fox jumps over 2 lazy dogs." ;
let regex = / \d+ / g ;
let matches = str. match ( regex) ;
console. log ( matches) ;
replace(searchValue, newValue): 在字符串中查找匹配项并替换为新的值
let str = "Hello World!" ;
console. log ( str. replace ( "World" , "Vue" ) ) ;
search(regexp): 使用正则表达式搜索字符串,并返回匹配项的索引
let str = "Hello World!" ;
console. log ( str. search ( / World / ) ) ;
slice(beginIndex, endIndex): 提取字符串的片段,并在新的字符串中返回被提取的部分
let str = "Hello World!" ;
console. log ( str. slice ( 0 , 5 ) ) ;
split(separator, limit): 通过将字符串分割成子字符串数组,来将一个字符串分割成字符串数组
let str = "Hello World!" ;
console. log ( str. split ( " " ) ) ;
substring(start, end): 提取字符串中两个指定的索引号之间的字符
let str = "Hello World!" ;
console. log ( str. substring ( 0 , 5 ) ) ;
toLowerCase()和toUpperCase(): 将字符串转换为小写/大写
let str = "Hello World!" ;
console. log ( str. toLowerCase ( ) ) ;
console. log ( str. toUpperCase ( ) ) ;
toLocaleLowerCase() 和 toLocaleUpperCase():根据本地主机的语言环境将字符串转换为小写或大写。考虑了本地化的特殊字符
let str = "STRİNG" ;
console. log ( str. toLocaleLowerCase ( 'tr-TR' ) ) ;
console. log ( str. toLocaleUpperCase ( 'tr-TR' ) ) ;
trim(): 去除字符串两端的空白字符
let str = " Hello World! " ;
console. log ( str. trim ( ) ) ;
startsWith(searchString, position) 和endsWith(searchString, length):: 去除字符串两端的空白字符
let str = "Hello World!" ;
console. log ( str. startsWith ( "Hello" ) ) ;
console. log ( str. endsWith ( "!" ) ) ;
substr(start, length): 从起始索引号提取指定长度的子字符串
let str = "Hello World!" ;
console. log ( str. substr ( 7 , 5 ) ) ;
repeat(count): 返回一个新字符串,该字符串包含将原字符串重复指定的次数后的结果
let str = "Hello" ;
console. log ( str. repeat ( 3 ) ) ;
localeCompare(that, locales, options): 比较两个字符串,并返回用于表示两个字符串在排序顺序中的相对位置的数字
let str1 = "apple" ;
let str2 = "Banana" ;
console. log ( str1. localeCompare ( str2) ) ;
padStart(targetLength, padString) 和 padEnd(targetLength, padString): 在当前字符串的开始或末尾填充指定的字符串,直到达到目标长度。如果不指定填充字符串,则默认使用空格填充
let str = "123" ;
console. log ( str. padStart ( 5 , "0" ) ) ;
console. log ( str. padEnd ( 7 , "!" ) ) ;