字符串拓展

字符串的拓展

模板字符串

  • 反點(diǎn)
console.log(` \` `);
//在模板字符串中如果需要寫一個(gè)字符反點(diǎn),則要在反點(diǎn)前面加上\
  • 字符串的拓展方法
let str='hellow';
  • 1.includes();
console.log(str.includes("o"));//true
console.log(str.includes("a"));//false
// 查找指定字符有返回值
// 如果能找到返回true,找不到返回false
  • 2.startsWith();
console.log(str.startsWith("h"));//true
console.log(str.startsWith("he"));//true
console.log(str.startsWith("hel"));//true
console.log(str.startsWith("helo"));//false
console.log(str.startsWith("o"));//false
console.log(str.startsWith("w"));//false
// 判斷是否以指定字符開頭返回值是布爾值
// 是返回TRUE
// 不是返回false
  • 3.endWith();
console.log(str.endsWith("w"));
console.log(str.endsWith("ow"));
console.log(str.endsWith("oll"));
console.log(str.endsWith("ll"));
// 判斷是否以指定字符結(jié)尾返回值是布爾值
// 是返回TRUE
// 不是返回false
  • 4.repeat();
console.log(str.repeat(1));
console.log(str.repeat(2));
console.log(str.repeat(3));
// 將原字符串重復(fù)復(fù)制指定次數(shù),并將生成的新字符串返回
  • 5.trim();
let str1 = ' a b c d e f ';
console.log(str1.trim());//刪除首尾空格
  • 6.trimStart();
let str1 = ' a b c d e f ';
console.log(str1.trimStart());//刪除首位空格
  • 7.trimEnd();
let str1 = ' a b c d e f ';
console.log(str1.trimEnd());//刪除末位空格
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容