String
concat
var firstName = 'Jennifer'
var lastName = ' Lawrence'
var holeName = firstName.concat(lastName);
// "Jennifer Lawrence"
slice
var str = "hello world";
var result = str.slice(3,7);
// lo w 參數(shù)中第一個作起始位置
// 對于負參,加上length
// [)
substring
var str = "hello world";
var result = str.slice(7,3);
// lo w 參數(shù)中較小的作為起始位置
// 對于負參,變0
// [)
substr
var str = "hello world";
var result = str.slice(3,7);
// lo worl 第二個參數(shù)作截取的長度
// 僅對于第一個負參,加上length
// [)
indexOf lastIndexOf
var str = "hello world";
var result1 = str.indexOf("o") // 4
var result2 = str.lastIndexOf("o") // 7
var result3 = str.indexOf("o",6) // 7
var result4 = str.lastIndexOf("o",6) // 4
trim
var str = " hello world ";
var result = str.trim()
// hello world
replace
var str = "cat,bat,sat,fat";
var result = str.replace("at","one");
// cone,bat,sat,fat
// 只替換第一個
split
var str = "red,blue,green,yellow";
var res1 = str.split(",")
// ["red", "blue", "green", "yellow"]
var res2 = str.split(",", 2);
// ["red", "blue"] 第二個參數(shù)用來限制數(shù)組大小
join
var arr = ["A", "B", "C", "D"]
var result = arr.join("->")
// A->B->C->D
for of 遍歷字符串
for (let codePoint of 'foo') {
console.log(codePoint)
}
startsWith endsWith includes
var s = 'Hello world!';
s.startsWith('world', 6) // true
s.endsWith('Hello', 5) // true
s.includes('Hello', 6) // false
at
var result = 'abc'.charAt(0) // b
padStart padEnd
var pad1 = "x".padStart(5, 'ab') // 'ababx'
var pad2 = "x".padStart(4, 'ab') // 'abax'
// 頭部補全
var pad3 = "x".padEnd(5, 'ab') // 'xabab'
var pad4 = "x".padEnd(4, 'ab') // 'xaba'
// 尾部補全