ES6入門教程——8、ES6字符串

一、一般用法

let string = "apple,banana,orange";

string.includes("banana"); // true

string.startsWith("apple"); // true

string.endsWith("apple"); // false

string.startsWith("banana",6) // true

二、字符串重複

console.log("Hello,".repeat(3));  // "Hello,Hello,Hello,"
console.log("Hello,".repeat(-0.5));  // "" 

三、字符串補全

console.log("h".padStart(5,"o"));  // "ooooh"
console.log("h".padEnd(5,"o"));    // "hoooo"
console.log("h".padStart(5));      // "    h"

四、模板字符串

多行的寫法:

let string1 = `Hey,

can you stop angry now?`;

console.log(string1); // Hey, // can you stop angry now?

五、標籤模板

alert`Hello world!`;

// 等價於

alert('Hello world!');

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章