String.raw
String.raw
is a label function template string (if you do not know what label function, point ⚑), which returns the template string of original string . For example,\n
will not be treated as a newline, but will be treated as two ordinary characters\
andn
:console.log(String.raw`1\n2`); //1\n2
Usually, we don't need to call it as a normal function, but if you must call it as a normal function, pay attention to the format of the incoming parameters:
(template: {
raw: readonly string[] | ArrayLike<string>;
}, ...substitutions: any[])=>string
String.raw
first argument must be a raw
object attributes, the attribute value character array or character class array , the value of remaining parameters is inserted.
console.log(String.raw({ raw: 'abcd' }, 0, 1, 2)); //a0b1c2d
//3 4两个插值超出了模板数组的范围,会被忽略
console.log(String.raw({ raw: ['a', 'b', 'c', 'd'] }, 0, 1, 2, 3, 4)); //a0b1c2d
String.prototype.repeat
Function type:
(count?:number)=>string
repeat
function according to the parameters passed n
, a return to the original string repeated n
times new string , parameters n
default 0
.
//传入0或者不传参数时,原字符串重复0次,返回空字符串
console.log('a'.repeat(0));// ''
console.log('a'.repeat(2));// 'aa'
String.prototype.startsWith,String.prototype.endsWith,String.prototype.includes
Function type:
(queryString?:string,startPosition?:number)=>boolean
startsWith
, endsWidth
and includes
functions are new functions for checking strings in ES6. They receive two parameters. The first parameter queryString
is the string to be queried in the original string, and the second parameter startPostion
is the start query s position. In includes
and startsWith
, startPosition
defaults to 0
, in endsWith
, startPostion
defaults to original string length
.
console.log('abcde'.includes('a')); //true
//改变起始位置
console.log('abcde'.includes('a', 1)); //false
console.log('abcde'.startsWith('a')); //true
//改变起始位置
console.log('abcde'.startsWith('a', 1)); //false
console.log('abcde'.endsWith('a')); //false
//改变起始位置
console.log('abcde'.endsWith('a', 1)); //true
It should be noted that when the string to be checked, that is, the parameter queryString
is empty string ''
, the return result of these three functions is always true
.
console.log('abcde'.includes('')); //true
//改变起始位置
console.log('abcde'.includes('', 1)); //true
console.log('abcde'.startsWith('')); //true
//改变起始位置
console.log('abcde'.startsWith('', 1)); //true
console.log('abcde'.endsWith('')); //true
//改变起始位置
console.log('abcde'.endsWith('', 1)); //true
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。