ES6-字符串函数

2018-06-06  本文已影响0人  闪电西兰花
const id = '32015619620508554x';
const fan = 'I like php.';
//startsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“开头”的,根据判断结果返回 true 或 false
const jiangSu = id.startsWith('32');              //true,判断是否以32开头
const is1962 = id.startsWith('1962',6);           //true,字符串从第6位开始是否匹配,默认值为0   
const capital = fan.startsWith('i');              //false,区分大小写
//endsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的
const end = id.endsWith('x');                     //true,字符串是否已x结尾
const endCapital = id.endsWith('X');              //false,同上,但区分大小写              
const like = fan.endsWith('like',6);              //true,字符串的真正第6位,结束位置的默认值为str.length
const isPhp = fan.includes('php');                //true,判断字符串中是否包含php
const afterIsPhp = fan.includes('php',9);         //false,字符串的第9位开始是否包含php
const head = `${'='.repeat(5)}${fan}${'='.repeat(5)}`;         ////=====I like php=====
//右对齐显示字符串
function center(string,length=25){
    return `${' '.repeat(Math.max(length-string.length,0))}${string}`
}
flush right.PNG
上一篇 下一篇

猜你喜欢

热点阅读