repeat(s,n):表示將字串重複指定次數返回新的字串。
let a =
'xu'
.repeat(2
)let b =
'dan'
.repeat
(2.6
)// -0.2會進行取整運算,等同重複次數0,返回""
let c =
'feng'
.repeat(-
0.2)
// nan 等同重複次數0,返回""
let c =
'feng'
.repeat
(nan
)// xuxu dandan
console.
log(a,b,c)
// 會報錯,無法處理 <=-1
let d =
'h'.
repeat(-
1)
模板字串相當於加強版的字串,用反引號 `,除了作為普通字串,還可以用來定義多行字串,還可以在字串中加入變數和表示式,還可以呼叫函式。
// 普通字串
let str =
`hello\nworld`
//多行字串
let str2 =
`hello,
xudanfeng`
// 插入變數和表示式
let str3 =
`哈哈
$,你好$`
// 呼叫函式
let str4 =
`i am $`
function
message()
// 普通字串
let str =
`helloworld`
let l1 = str.
includes
('ll'
)let l2 = str.
startswith
('he'
)let l3 = str.
startswith
('ll',2
)let l4 = str.
endswith
('or'
)//true true true false
console.
log(l1, l2, l3, l4)
ES6 字串擴充套件
1 字串可以使用 u x的形式來表達乙個字元,x叫做字元的碼點,x的範圍是0000 ffff,超過ffff的碼點需要用兩個雙位元組表示 如果我們 u後面的16進製制的值大於ffff,我們需要加乙個大括號 u讓js正確解析。2 我們應該都了解,漢字一般都需要兩個雙位元組來表示,在js中兩個位元組佔乙個...
ES6 字串方法的擴充套件
repeat trimstart trimend replaceall 本文基於阮一峰的es6編寫,在此只介紹一些常用的。本來js中只有 indexof 方法可以用來確定乙個字串是否包含在另乙個字串中。es6中又新增了三種新方法。返回布林值,表示是否找到了引數字串。let str abcde str...
ES6陣列擴充套件運算子和字串遍歷的新方法!!!
該方法主要是讓我們在學習和工作中能夠更加方便的去運算元據,陣列和字串!下面開始 展示階段 1.擴充套件運算子 console.log 1,2,3 以上結果輸出 1,2,3 console.log 1,2,3,4 5 以上結果輸出 1,2,3,4,5 可以看出我在陣列的前面使用了三個點,這個方法就是e...