首页 > 代码库 > es6 字符串扩展

es6 字符串扩展

var s = ??;
var s1 = \ud842\uDFB7;
var s2 = \u{20BB7};

s1 === s2 //true

//js have 6 type express str
\z === z
//   ‘\172‘ === ‘z‘
\x7A === z
\u007A === z
\u{7A} === z

s.length = 2;
s.charAt(0) //‘?‘
s.charAt(1) //‘?‘
//charCodeAt
s.charCodeAt(0)
s.charCodeAt(1)
//codePointAt 用来处理4个字节的字符,返回一个字符的码点
var s =  ?? a
s.codePointAt(0)  //32
s.codePointAt(1)  //134071
s.codePointAt(2)  //57271
s.codePointAt(3)  //32
s.codePointAt(4)  //97
s.codePointAt(5)  //undefined

//String.fromCodePoint()   返回字符串
var text = String.fromCodePoint(0x20BB7) //"??"

//字符串的遍历器接口
for (var s of foo){console.log(foo)}
// f
// o
// o
for(var i = 0 ; i < text.length; i++){
  console.log(text[i]);
}
//?
//?
for (let i of text){
  console.log(i)
}

text.at(0)  //代替code

//normalize() pass

//includes(),startsWith(),endsWith()
var str = Hello world
str.startWidth(world,6) //true
str.endsWith(Hello,5) //true
str.includes(Hello, 6)  //false

//repeat
str.repeat(3) //"Hello worldHello worldHello world"

//padStart()    padEnd()
x.padStart(5,a)
x.padStart(3,a)

x.padEnd(5, a)
x.padEnd(3, a)

 

es6 字符串扩展