总结:整理与字符串相关的知识点。
主要有
1.对字符串的查找,如是否有某些字符,字符在哪一个位置,根据位置去查找字符
2.对字符串的删除,截取咱们想要的字符,也多是删除咱们不想要的字符
3.对字符串的增长 固定格式的补全 自动补全 重复补全 拼接
4.对字符串的替换,对某些字符的所有替换 或者说仅对第一个出现的字符替换
5.转换 大小写的转换 转为码点 码点转字符
6.不一样类型的转换;数字与字符串的转换 数组与字符串的转换
7.比较 比较大小 比较某些字符出现的位置顺序es6
含义 | 方法 | 返回值 | 改变原字符串 |
---|---|---|---|
查找 | indexOf() | 位置 | no |
查找 | search() | 位置 | yes |
查找 | includes() | boolean | no |
查找 | startswith()头部有? | boolean | yes |
查找 | endsWith() 尾部有? | boolean | yes |
截取 | substr(start,length) | 新字符串 | no |
截取 | substring(start,stop) | 新字符串 | no |
截取 | slice(start,stop) | 新字符串 | no |
去空格 | trim() | ** | no |
重复次数 | repeat(n) | 新字符串 | no |
补全 | padStart(length,value) 头部补全 | 新字符串 | no |
补全 | padEnd(length,value)尾部补全 | 新字符串 | no |
匹配 | match() | 查找项+位置 | no |
正则的全匹配 | matchAll() | 正则 | yes |
替换 | replace() | 新字符串 | no |
转小写 | toUpperCase() | 新字符串 | yes |
转小写 | toLowerCase() | 新字符串 | yes |
转码点 | charCodeAt() | 新字符 | no |
转码点 | codePointAt() | 位置 | no |
转字符 | charAt() | 新字符 | no |
转字符 | at() 还未实现 | 位置 | no |
码点->字符 | String.fromCodePoint() | 新字符 | / |
比较位置顺序 | localeCompare(a,b) | 1/-1/0 | no |
** | normalize() | ** | no |
如是否有某些字符,某个字符的位置是哪里? 第n 位的字符是什么正则表达式
var s = 'kljlk;juoi' s.charAt(2) // j s.charAt('j') // k s.charAt('15') // '' s[15] // undefined s[3] // l s.charCodeAt(2) // 106 s.codePointAt(2) // 106
- indexOf()-------返回位置,没有就返回-1 - lastIndexOf();---返回位置,从后开始查找 - includes():返回布尔值,表示是否找到了参数字符串。第二个参数,表示开始搜索的位置 - startsWith():返回布尔值,表示参数字符串是否在原字符串的头部。第二个参数,表示开始搜索的位置 - endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部第二个参数,表示开始搜索的位置 - match()---返回相关的信息--只接受一个参数,要么正则表达式 要么RegExp对象 - search()----返回位置,没有就返回-1---只接受一个参数,要么正则表达式 要么RegExp对象
let s = 'Hello world!'; s.startsWith('world', 6) // true s.endsWith('Hello', 5) // true s.includes('Hello', 6) // false var s1 = 'fdkfdkjldrm ' s1.indexOf('kjl') // 5 s1.lastIndexOf('kjl') // 5 s1.indexOf('fd') // 0 s1.lastIndexOf('fd') // 3 s1.search('fdlp') // -1 s1.search('fd') // 0 s1.search('kjl') // 5 s1.match('fd') // ["fd", index: 0, input: "fdkfdkjldrm ", groups: undefined] s1.match('f5') // null
如下4种方法不会修改字符串自己;json
2.1.2截取
slice(start,stop)
substring(start,stop)
substr(start,length)数组
**共同点:** 基于字符串建立新字符串,不改变原有字符串 第二个参数是可选的 无第二个参数,即字符结尾为结束位置 **不一样点** 对负数的处理: slice()---将负数和字符串长度相加 substring()----将负数转为0 substr()---将第一个负数和字符串长度相加。第二个负数转为0,即不截取
2.1.2 删除空格函数
trim() 删除字符串中 前置和后置的空格
repeat(n) --返回一个新字符串,表示将原字符串重复n次。
参数若是是小数,会被取整。编码
'x'.repeat(3) // "xxx" 'hello'.repeat(2) // "hellohello" 'na'.repeat(0) // "" 'na'.repeat(2.9) // "nana" 'na'.repeat(Infinity) // RangeError 'na'.repeat(-1) // RangeError 参数NaN等同于 0
padStart(minlength,string)用于头部补全,
padEnd(minlength,string)用于尾部补全
用途:es5
'x'.padStart(5, 'ab') // 'ababx' 'x'.padStart(4, 'ab') // 'abax' 'x'.padEnd(5, 'ab') // 'xabab' 'x'.padEnd(4, 'ab') // 'xaba' 'xxx'.padStart(2, 'ab') // 'xxx' 'xxx'.padEnd(2, 'ab') // 'xxx' 'x'.padStart(4) // ' x' 'x'.padEnd(4) // 'x ' '12'.padStart(10, 'YYYY-MM-DD') // "YYYY-MM-12" '09-12'.padStart(10, 'YYYY-MM-DD') // "YYYY-09-12"
- toLowerCase();-----小写 - toUpperCase();----变大写 - toLocaleLowerCase(); - toLocaleUpperCase();
根据码点返回对应的字符spa
String.fromCharCode(100) "d" String.fromCodePoint(0x20BB7) // "?" String.fromCodePoint(100,100,100) // ddd
array.join(',') ------将数组转字符串
string.split(',') ---------字符串转数组3d
number --->string(4种)code
1.Number() 2.parseInt() 3.parseFloat() 4.+string-----
var s = '23' var k = +s typeof k // number
number---> string(4种)
String(number)--强制转换 toString(8进制) toFixed(n)---//数字转换为字符串,而且显示小数点后的指定的位数 number + ''
var s = 123.68 var k= s.tiString(8) var k0 = s.toFixed(1) var k1 = String(s) k // '173.43656' k0 // '123.7' k1 // '123.68'
JSON.parse() ------ json 字符串 转 json 对象
JSON.stringify() -----json 对象转 json 字符串
localeCompare()---比较2个字符串,参数应该在比较的字符串前面,则返回1;后面,则 -1; 等于,则 0;
var s= 'dfhk' s.localeCompare('fgfg') // -1 s.localeCompare('afgfg') // 1 s.localeCompare('dfhk') // 0 s.localeCompare('df') // 1
与es5的比较
for循环虽能够遍历字符串,但不能识别大于oxFFFF的编码;
1.
function showCase(value) { switch(value) { case 'A': console.log('Case A'); break; case 'B': console.log('Case B'); break; case undefined: console.log('undefined'); break; default: console.log('Do not know!'); } } showCase(new String('A')); 结果是 ‘'Do not know!’
2.
function showCase2(value) { switch(value) { case 'A': console.log('Case A'); break; case 'B': console.log('Case B'); break; case undefined: console.log('undefined'); break; default: console.log('Do not know!'); } } showCase2(String('A')); 结果是‘Case A’
3.
'5' + 3
'5' - 3
// 53 2; because:Strings know about + and will use it, but they are ignorant of - so in that case the strings get converted to numbers.
4.
3.toString() 3..toString() 3...toString() // error '3' error;because:1.1, 1., .1 都是合法的数字. 那么在解析 3.toString 的时候这个 . 究竟是属于这个数字仍是函数调用呢? 只能是数字, 由于3.合法啊!