blob: 8b00505050f64561fca10e980c908f0b39a4a638 (
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
|
---
title: String.prototype.padStart()
slug: Web/JavaScript/Reference/Global_Objects/String/padStart
tags:
- Advanced
- Intermediate
- JavaScript
- Method
- Prototype
- Reference
- String
translation_of: Web/JavaScript/Reference/Global_Objects/String/padStart
---
<div>{{JSRef}}</div>
<p><span class="seoSummary"><strong><code>padStart()</code></strong> メソッドは、結果の文字列が指定した長さになるように、現在の文字列を他の文字列で (必要に応じて繰り返して) 延長します。</span>延長は、現在の文字列の先頭から適用されます。</p>
<div>{{EmbedInteractiveExample("pages/js/string-padstart.html")}}</div>
<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div>
<h2 id="Syntax" name="Syntax">構文</h2>
<pre class="syntaxbox notranslate"><var>str</var>.padStart(<var>targetLength</var> [, <var>padString</var>])</pre>
<h3 id="Parameters" name="Parameters">引数</h3>
<dl>
<dt><code><var>targetLength</var></code></dt>
<dd>現在の <code><var>str</var></code> の延長後に返される文字列の長さです。この値が <code><var>str.</var>length</code> よりも短い場合、 <code><var>str</var></code> がそのまま返されます。</dd>
<dt><code><var>padString</var></code> {{optional_inline}}</dt>
<dd>現在の <code><var>str</var></code> を延長するための文字列です。 <code><var>padString</var></code> が <code><var>targetLength</var></code> より長い場合は、右からその長さ分が切り捨てられます。この引数の既定値は、 "<code> </code>" (<code>U+0020 'SPACE'</code>) です。</dd>
</dl>
<h3 id="Return_value" name="Return_value">返値</h3>
<p>現在の文字列の先頭に <code><var>padString</var></code> が適用された、指定された <code><var>targetLength</var></code> の長さの {{jsxref("String")}} です。</p>
<h2 id="Examples" name="Examples">例</h2>
<h3 id="Basic_examples" name="Basic_examples">基本的な例</h3>
<pre class="brush: js notranslate">'abc'.padStart(10); // " abc"
'abc'.padStart(10, "foo"); // "foofoofabc"
'abc'.padStart(6,"123465"); // "123abc"
'abc'.padStart(8, "0"); // "00000abc"
'abc'.padStart(1); // "abc"</pre>
<h3 id="Fixed_width_string_number_conversion" name="Fixed_width_string_number_conversion">数値を固定長文字列へ変換</h3>
<pre class="brush: js notranslate">// Javascript version of: (unsigned)
// printf "%0*d" width num
function leftFillNum(num, targetLength) {
return num.toString().padStart(targetLength, 0);
}
const num = 123;
console.log(leftFillNum(num, 5));
// expected output: "00123"
</pre>
<h2 id="Specifications" name="Specifications">仕様書</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">仕様書</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-string.prototype.padstart', 'String.prototype.padStart')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
<p>{{Compat("javascript.builtins.String.padStart")}}</p>
<h2 id="See_also" name="See_also">関連情報</h2>
<ul>
<li>{{jsxref("String.prototype.padEnd()")}}</li>
<li><a href="https://github.com/uxitten/polyfill/blob/master/string.polyfill.js">A polyfill</a></li>
</ul>
|