ES6 新增 api
type | 作用 | 解釋 |
---|---|---|
includes(str, index) | 確定一個字符串是否包含在另一個字符串中 | 返回布爾值背捌,表示是否找到了參數(shù)字符串。 |
startsWith(str, index) | 確定一個字符串是否包含在另一個字符串中 | 返回布爾值慢洋,表示參數(shù)字符串是否在原字符串的頭部。 |
endsWith(str, n) | 確定一個字符串是否包含在另一個字符串中 | 返回布爾值,表示參數(shù)字符串是否在原字符串的尾部阶剑。 |
repeat(n) | 將原字符串重復 n 次。 (n 只能是非負整數(shù)危号,(-1,0)區(qū)間的數(shù)會認為是 0,小數(shù)會被取整 2.9 -> 2),其他 undefined NaN 同樣會先執(zhí)行 Boolean() | 返回一個新字符串 |
padStart(n,str) | 如果某個字符串不夠指定長度牧愁,會在頭部補全 | 返回一個新字符串 |
padEnd(n,str) | 如果某個字符串不夠指定長度,會在頭部或尾部補全 | 返回一個新字符串 |
例子
let s = "Hello world!";
s.startsWith("world", 6); // true
s.endsWith("Hello", 5); // true
s.includes("Hello", 6); // false
s.repeat(3); // "hello worldhello worldhello world"
上面代碼表示外莲,使用第二個參數(shù) n 時猪半,endsWith 的行為與其他兩個方法有所不同。它針對前 n 個字符偷线,而其他兩個方法針對從第 n 個位置直到字符串結(jié)束磨确。
回到目錄