JavaScript 中 str.padStart() 方法的重要性是什麼?
我們可以使用 **concat()** 方法連線兩個字串。但是,如果我們需要在第一個字串的開頭附加一個特定的字串,那麼最簡單的方法是使用 **string.padStart()**。此方法不僅將第二個字串新增到第一個字串的開頭,而且還會處理要新增的字元數量。它基本上接受兩個引數,一個是 **長度**,另一個是 **第二個字串**。**string.padStart()** 方法根據提供的長度將第二個字串新增到第一個字串中。
語法
string.padStart(length,"string");
它將 **長度** 作為引數,以將結果字串限制為這麼多字元。
它接受另一個字串,將其附加到提供的字串。
示例
<html> <body> <script> var str = "the best"; var st = "Hello" document.write(st.padStart(24," glad to meet you, ")); document.write("</br>"); document.write(str.padStart(16, "Tutorix ")); </script> </body> </html>
輸出
glad to meet you, Hello Tutorix the best
當第一個字串的大小大於提供的長度時,原始字串將不會發生任何更改,並且原始字串將顯示為輸出。
示例
<html> <body> <script> var str = "Tutorix is the best e-learning platform best"; document.write(str.padStart(16, "Tutorix ")); </script> </body> </html>
輸出
Tutorix is the best e-learning platform best
廣告