ES6中字符串新增的幾個(gè)常用方法說明
一.includes
說明:判斷字符串中是否包含給定值,返回值為bool類型。
示例1:"hello world".includes("hello"); //true
示例2:"hello world".includes("hello", 1); //false 第二個(gè)參數(shù)代表從下標(biāo)為1的地方開始判斷,直到字符串結(jié)束為止。
二.startsWith
說明:判斷字符串是否以給定值開始,返回值為bool類型。
示例1:"hello world".startsWith("hello"); //true
示例2:"hello world".startsWith("hello", 1); //false 第二個(gè)參數(shù)代表從下標(biāo)為1的地方開始判斷,直到字符串結(jié)束為止。
三.endsWith
說明:判斷字符串是否以給定值結(jié)束,返回值為bool類型。
示例1:"hello world".endsWith("world"); //true
示例2:"hello world".endsWith("world", 5); //false 第二個(gè)參數(shù)代表截取字符串前5位進(jìn)行判斷。
四.padStart
說明:從頭部開始自動(dòng)補(bǔ)齊,直至達(dá)到指定長(zhǎng)度。
示例1:let hw = "world".padStart(12, " hello"); // hw = " hello world" 假如給定字符串一次不足以補(bǔ)齊,則會(huì)重復(fù)補(bǔ)齊
示例2:let hw = "world".padStart(11, "hello HZ"); // hw = "hello world" 假如給定字符串長(zhǎng)度過大,則只截取符合要求的長(zhǎng)度
五.padEnd
說明:從尾部開始自動(dòng)補(bǔ)齊,直至達(dá)到指定長(zhǎng)度。
示例1:let hw = "hello".padStart(12, " world"); // hw = "hello world " 假如給定字符串一次不足以補(bǔ)齊,則會(huì)重復(fù)補(bǔ)齊
示例2:let hw = "hello".padStart(11, " world is beautiful"); // hw = "hello world" 假如給定字符串長(zhǎng)度過大,則只截取符合要求的長(zhǎng)度
六.repeat
說明:重復(fù)原字符串n次。
示例:let hw = "hello world ".repeat(3); // hw = "hello world hello world hello world "