aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html
blob: 8596df0ae3b6f681f4d9360b440d9330a423328a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
---
title: String.prototype.padStart()
slug: Web/JavaScript/Reference/Global_Objects/String/padStart
tags:
  - ECMAScript 2017
  - JavaScript
  - Method
  - Reference
  - String
  - String.padStart()
translation_of: Web/JavaScript/Reference/Global_Objects/String/padStart
---
<div>{{JSRef}}</div>

<p><strong><code>padStart()</code></strong> 方法用另一个字符串填充当前字符串(如果需要的话,会重复多次),以便产生的字符串达到给定的长度。从当前字符串的左侧开始填充。</p>

<div>{{EmbedInteractiveExample("pages/js/string-padstart.html")}}</div>

<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox notranslate"><var>str</var>.padStart(<var>targetLength</var> [, <var>padString</var>])</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>targetLength</code></dt>
 <dd>当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度,则返回当前字符串本身。</dd>
 <dt><code>padString</code> {{optional_inline}}</dt>
 <dd>填充字符串。如果字符串太长,使填充后的字符串长度超过了目标长度,则只保留最左侧的部分,其他部分会被截断。此参数的默认值为 " "(U+0020)。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>在原字符串开头填充指定的填充字符串直到目标长度所形成的新字符串。</p>

<h2 id="示例">示例</h2>

<pre class="brush: js line-numbers  language-js notranslate"><code class="language-js">'abc'.padStart(10);         // "       abc"
'abc'.padStart(10, "foo");  // "foofoofabc"
'abc'.padStart(6,"123465"); // "123abc"
'abc'.padStart(8, "0");     // "00000abc"
'abc'.padStart(1);          // "abc"</code></pre>

<h2 id="Polyfill">Polyfill</h2>

<p>如果原生环境不支持该方法,在其他代码之前先运行下面的代码,将创建 <code>String.prototype.padStart()</code> 方法。</p>

<pre class="brush: js line-numbers  language-js notranslate"><code class="language-js">// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
if (!String.prototype.padStart) {
    String.prototype.padStart = function padStart(targetLength,padString) {
        targetLength = targetLength&gt;&gt;0; //floor if number or convert non-number to 0;
        padString = String((typeof padString !== 'undefined' ? padString : ' '));
        if (this.length &gt; targetLength) {
            return String(this);
        }
        else {
            targetLength = targetLength-this.length;
            if (targetLength &gt; padString.length) {
                padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
            }
            return padString.slice(0,targetLength) + String(this);
        }
    };
}</code>
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-string.prototype.padstart', 'String.prototype.padStart')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>在 ECMAScript 2017 中首次被定义。</td>
  </tr>
  <tr>
   <td>{{SpecName('ES8', '#sec-string.prototype.padstart', 'String.prototype.padStart')}}</td>
   <td>{{Spec2('ES8')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("javascript.builtins.String.padStart")}}</p>

<h2 id="另请参阅">另请参阅</h2>

<ul>
 <li>{{jsxref("String.prototype.padEnd()")}}</li>
</ul>