字符串办法
String类型为根本类型
,对其的增删改查都不扭转原字符串
,返回一个新字符串再进行操作
提取
substring
(index1,index2)substr
(start,length)slice
(start,end)split
(str)
substring(index1,index2)
let s = "Hello World"s.substring() //Hello Worlds.substring(5) // Worlds.substring(3, -5) //Hel
没有参数
:返回原字符串一个参数
:作为起始下标,完结默认为字符串长度两个参数
:主动比拟
辨别起始与完结正数
:疏忽或按索引0解决- 提取的字符串
包头不包尾
substr(start,length)
let s = "Hello World"s.substr(3,1) //ls.substr(-3, 2) //rl
0个参数
:与substring性能一样1个参数
:反对正数为起始索引两个参数
:第一个为起始,第二个为长度正数
:start
反对正数,-1为最初一个字符,length为正数时返回空字符串
slice(start,end)
let s = "Hello World"s.slice(3, -6) //lo
0、1个参数
与substr雷同两个参数
:start与end均反对正数
,但end的地位不能在start前,否则返回空字符串
split(str)
let s = "Hello World"s.split() //["Hello World"]s.split('l') //["He","","o Wor","d"]
- 将字符串转化为
数组
- 以子串或空字符串为
分隔
,划分为多个子串,将这些子串放入数组,并返回该数组
搜寻
indexOf
(str,start)lastIndexOf
(str,start)startsWinth
(str)endsWith
(str)includes
(str)charAt
(index)charCodeAt
(index)codePointAt
(index)
indexOf(str,start)
let s = "Hello World"s.indexOf() //-1s.indexOf("l") //2s.indexOf("l", 6) //9
- 无参数或者不存在子串,返回
-1
两个参数
:start为正数
时疏忽或默认为0,空字符串
返回start
lastIndexOf(str,start)
let s = "Hello World"s.lastIndexOf("l") //9s.lastIndexOf("l", 4) //3
- 反向检索,返回第一个匹配的下标
- 正数根本无奈应用
startsWith(str) endsWith(str)
let s = "Hello World"s.startsWith() //falses.startsWith("") //trues.startsWith("H") //true
- 检测是否以某子串结尾或结尾,返回true/false
空字符串
返回true
includes(str)
let s = "Hello World"s.includes() //falses.includes("") //trues.includes("ll") //true
- 检测是否蕴含子串,返回true/false
- 空字符串返回true
charAt(index)
let s = "Hello World"s.charAt() //Hs.charAt(0) //H
- 检索字符串中某下标下的字符
- 默认为0,正数或者超出长度返回空字符串
charCodeAt(index) codePointAt(index)
let s1 = "Hello World"s1.charCodeAt() //72s1.charCodeAt(0) //72let s2 = ""s.charCodeAt() //55362s.codePointAt()//134071
- 返回指定地位的字符的
Unicode
编码 - 默认为0,正数或超出返回
NaN
- charCodeAt返回0 - 65535 两个字节
- codePointAt能返回四个字节的码点
批改
replace
(str1,str2)toLowerCase
()toUpperCase
()trim
()trimStart
()trimEnd
()padStart
(length,str)padEnd
(length,str)normalize
("NFC/NFD/NFKC/NFKD")
replace(str1,str2)
let s = "Hello World"s.replace() //Hello Worlds.replace("World", "JS") //Hello JS
- 将字符串中的子串替换为其余字符串
- str1不是原字符串的子串时,返回原字符串
toLowerCase() toUpperCase()
let s = "Hello World"s.toLowerCase() //hello worlds.toUpperCase() //HELLO WORLD
- 切换大小写
trim() trimStart() trimEnd()
let s = " Hello World "s.trim() //"Hello World"s.trimStart()//"Hello World "s.trimEnd() //" Hello World"
- 删除两侧空格
padStart(length,str) padEnd(length,str)
let s = "Hello World"s.padStart() //Hello Worlds.padStart(s.length,"*") //Hello Worlds.padStart(s.length+2,"*") //**Hello World
- 在字符串的结尾或结尾填充字符串以达到指定长度
- 指定长度须要大于原字符串长度
- 当填充字符串过长,其长度与原字符串长度加和大于指定长度,则只填充一部分
normalize("NFC/NFD/NFKC/NFKD")
let s = "\u00F1"s.normalize() //n
- 依照指定的一种 Unicode 正规模式将以后字符串正规化,默认为NFC
拼接
concat
(str)repeat
(times)
concat(str)
let s = "Hello World"let arr = ["1", "2"]console.log(s.concat(arr)); //Hello World1,2
- 将str拼接到s上,如果str不是字符串先转换成字符串再拼接
repeat(times)
let s = "Hello World"s.repeat() //""s.repeat(2)//Hello WorldHello World
- 无参数或参数为0返回空字符串
- times为小数时,向下取整