
- Javascript 基礎教程
- Javascript - 首頁
- JavaScript - 路線圖
- JavaScript - 概述
- JavaScript - 特性
- JavaScript - 啟用
- JavaScript - 位置
- JavaScript - 語法
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - 註釋
- JavaScript - 變數
- JavaScript - let 語句
- JavaScript - 常量
- JavaScript - 資料型別
- JavaScript - 型別轉換
- JavaScript - 嚴格模式
- JavaScript - 保留關鍵字
- JavaScript 運算子
- JavaScript - 運算子
- JavaScript - 算術運算子
- JavaScript - 比較運算子
- JavaScript - 邏輯運算子
- JavaScript - 位運算子
- JavaScript - 賦值運算子
- JavaScript - 條件運算子
- JavaScript - typeof 運算子
- JavaScript - 空值合併運算子
- JavaScript - delete 運算子
- JavaScript - 逗號運算子
- JavaScript - 分組運算子
- JavaScript - yield 運算子
- JavaScript - 展開運算子
- JavaScript - 指數運算子
- JavaScript - 運算子優先順序
- JavaScript 控制流
- JavaScript - if...else
- JavaScript - while 迴圈
- JavaScript - for 迴圈
- JavaScript - for...in
- Javascript - for...of
- JavaScript - 迴圈控制
- JavaScript - break 語句
- JavaScript - continue 語句
- JavaScript - switch case
- JavaScript - 使用者定義迭代器
- JavaScript 函式
- JavaScript - 函式
- JavaScript - 函式表示式
- JavaScript - 函式引數
- JavaScript - 預設引數
- JavaScript - Function() 建構函式
- JavaScript - 函式提升
- JavaScript - 自執行函式
- JavaScript - 箭頭函式
- JavaScript - 函式呼叫
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - 閉包
- JavaScript - 變數作用域
- JavaScript - 全域性變數
- JavaScript - 智慧函式引數
- JavaScript 物件
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - String
- JavaScript - Array
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Handler
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Set
- JavaScript - WeakSet
- JavaScript - Map
- JavaScript - WeakMap
- JavaScript - 可迭代物件
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - 模板字面量
- JavaScript - 標籤模板
- 面向物件的 JavaScript
- JavaScript - 物件
- JavaScript - 類
- JavaScript - 物件屬性
- JavaScript - 物件方法
- JavaScript - 靜態方法
- JavaScript - 顯示物件
- JavaScript - 物件訪問器
- JavaScript - 物件建構函式
- JavaScript - 原生原型
- JavaScript - ES5 物件方法
- JavaScript - 封裝
- JavaScript - 繼承
- JavaScript - 抽象
- JavaScript - 多型
- JavaScript - 解構賦值
- JavaScript - 物件解構
- JavaScript - 陣列解構
- JavaScript - 巢狀解構
- JavaScript - 可選鏈
- JavaScript - 全域性物件
- JavaScript - Mixin
- JavaScript - Proxy
- JavaScript 版本
- JavaScript - 歷史
- JavaScript - 版本
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript 非同步
- JavaScript - 非同步
- JavaScript - 回撥函式
- JavaScript - Promise
- JavaScript - Async/Await
- JavaScript - 微任務
- JavaScript - Promisification
- JavaScript - Promise 鏈式呼叫
- JavaScript - 定時事件
- JavaScript - setTimeout()
- JavaScript - setInterval()
- JavaScript Cookie
- JavaScript - Cookie
- JavaScript - Cookie 屬性
- JavaScript - 刪除 Cookie
- JavaScript 瀏覽器 BOM
- JavaScript - 瀏覽器物件模型
- JavaScript - Window 物件
- JavaScript - Document 物件
- JavaScript - Screen 物件
- JavaScript - History 物件
- JavaScript - Navigator 物件
- JavaScript - Location 物件
- JavaScript - Console 物件
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript 事件
- JavaScript - 事件
- JavaScript - DOM 事件
- JavaScript - addEventListener()
- JavaScript - 滑鼠事件
- JavaScript - 鍵盤事件
- JavaScript - 表單事件
- JavaScript - Window/Document 事件
- JavaScript - 事件委託
- JavaScript - 事件冒泡
- JavaScript - 事件捕獲
- JavaScript - 自定義事件
- JavaScript 錯誤處理
- JavaScript - 錯誤處理
- JavaScript - try...catch
- JavaScript - 除錯
- JavaScript - 自定義錯誤
- JavaScript - 擴充套件錯誤
- JavaScript 重要關鍵字
- JavaScript - this 關鍵字
- JavaScript - void 關鍵字
- JavaScript - new 關鍵字
- JavaScript - var 關鍵字
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM 方法和屬性
- JavaScript - DOM Document
- JavaScript - DOM 元素
- JavaScript - DOM 屬性 (Attr)
- JavaScript - DOM 表單
- JavaScript - 更改 HTML
- JavaScript - 更改 CSS
- JavaScript - DOM 動畫
- JavaScript - DOM 導航
- JavaScript - DOM 集合
- JavaScript - DOM NodeList
- JavaScript - DOM DOMTokenList
- JavaScript 其他
- JavaScript - Ajax
- JavaScript - 非同步迭代
- JavaScript - Atomics 物件
- JavaScript - rest 引數
- JavaScript - 頁面重定向
- JavaScript - 對話方塊
- JavaScript - 頁面列印
- JavaScript - 驗證
- JavaScript - 動畫
- JavaScript - 多媒體
- JavaScript - 圖片地圖
- JavaScript - 瀏覽器
- JavaScript - JSON
- JavaScript - 多行字串
- JavaScript - 日期格式
- JavaScript - 獲取日期方法
- JavaScript - 設定日期方法
- JavaScript - 模組
- JavaScript - 動態匯入
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - 淺複製
- JavaScript - 呼叫棧
- JavaScript - 引用型別
- JavaScript - IndexedDB
- JavaScript - 點選劫持攻擊
- JavaScript - 柯里化
- JavaScript - 圖形
- JavaScript - Canvas
- JavaScript - 防抖
- JavaScript - 效能
- JavaScript - 樣式指南
JavaScript padStart() 方法
JavaScript String padStart() 方法用於在當前字串的開頭填充(或擴充套件)指定的 padString,以達到給定的長度。如果必要,填充可以重複多次。如果我們不為該方法指定此可選引數 padString,它會在該字串的開頭新增空格作為填充,以達到指定的長度。
填充字串時,您會在字串開頭新增字元,直到其達到特定長度。
語法
以下是 JavaScript String padStart() 方法的語法:
padStart(targetLength, padString)
引數
此方法接受兩個名為“targetLength”和“padString”的引數,如下所述:
- targetLength - 新字串的長度。
- padString - 用於填充(擴充套件)當前字串的字串。
返回值
此方法返回一個指定 targetLength 的新字串,其中 padString 附加在開頭。
示例 1
如果省略 padString 引數,它將透過在當前字串的開頭新增空格作為填充來擴充套件字串,直到達到指定的 targetLength。
在下面的示例中,我們使用 JavaScript String padStart() 方法來填充(擴充套件)字串,在字串 "TutorialsPoint" 的開頭新增空格作為填充,直到達到指定的 targetLength 20。
<html> <head> <title>JavaScript String padStart() Method</title> </head> <body> <script> const str = "TutorialsPoint"; document.write("Original string: ", str); new_str = ""; document.write("<br>New string length before added padding: ",new_str.length); const targetLength = 20; document.write("<br>Target Length: ", targetLength); //using padStart() method new_str = str.padStart(targetLength); document.write("<br>New string after padding added to the begining of this string: ", new_str); document.write("<br>New string length after added padding: ",new_str.length); </script> </body> </html>
輸出
上述程式返回一個在字串 "TutorialsPoint" 開頭填充空格的新字串。
Original string: TutorialsPoint New string length before added padding: 0 Target Length: 20 New string after padding added to the begining of this string: TutorialsPoint New string length after added padding: 20
示例 2
如果將 padString 和 targetLength 引數都傳遞給此方法,它將透過在開頭新增 padString 來擴充套件字串,直到達到特定長度。
以下是 JavaScript String padStart() 方法的另一個示例。我們使用此方法透過在字串 "Hello World" 的開頭新增指定的 padString "Hi" 來擴充套件此字串,直到達到指定的 targetLength 25。
<html> <head> <title>JavaScript String padStart() Method</title> </head> <body> <script> const str = "Hello World"; document.write("Original string: ", str); new_str = ""; document.write("<br>New string length before added padding: ",new_str.length); const targetLength = 25; const padString = "Hi"; document.write("<br>Target Length: ", targetLength); document.write("<br>Pad string: ", padString); //using padStart() method new_str = str.padStart(targetLength, padString); document.write("<br>New string after padding added to the begining of this string: ", new_str); document.write("<br>New string length after added padding: ",new_str.length); </script> </body> </html>
輸出
執行上述程式後,它將返回一個在開頭新增填充的新字串。
Original string: Hello World New string length before added padding: 0 Target Length: 25 Pad string: Hi New string after padding added to the begining of this string: HiHiHiHiHiHiHiHello World New string length after added padding: 25
廣告