aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/string/length/index.html
blob: 62c3016f34a9138225cb4f40fd043685d103a3fe (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
101
102
103
---
title: String length
slug: Web/JavaScript/Reference/Global_Objects/String/length
tags:
  - JavaScript
  - Property
  - Prototype
  - Reference
  - String
  - String Length
  - length
translation_of: Web/JavaScript/Reference/Global_Objects/String/length
---
<div>{{JSRef}}</div>

<p><span class="seoSummary"><strong><code>length</code></strong> プロパティは {{jsxref("String")}} オブジェクトの文字列長を UTF-16 コードユニットの数で表します。 <code>length</code> は、 string インスタンスの読み取り専用データプロパティです。</span></p>

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

<h2 id="Description" name="Description">解説</h2>

<p>このプロパティは、文字列内のコード単位の数を返します。 JavaScript で使用される文字列書式である {{interwiki("wikipedia", "UTF-16")}} は、ほとんどの一般の文字は単一の16ビットコードユニットで表しますが、あまり使われない文字に対しては2つのコードユニットを使用する必要があり、 <code>length</code> で返される値が文字列の実際の文字数と一致しなくなる可能性があります。</p>

<p>ECMAScript 2016 (ed. 7) では最大長が <code>2^53 - 1</code> 要素と制定されました。それ以前は最大長は定まっていませんでした。 Firefox 内にある文字列の最大長は <code>2**30 - 2</code> (~1GB) です。Firefox 65 以前での最大長は <code>2**28 - 1</code> (~256MB) でした。</p>

<p>空の文字列の場合、<code>length</code> は 0 になります。</p>

<p>静的プロパティの <code>String.length</code> は文字列の長さとは関係なく、 <code>String</code> 関数のアリティ (ゆるく言えば、それが持つ形式的な引数の数) であり、 1 です。</p>

<h2 id="Unicode">Unicode</h2>

<p>`length` は文字数ではなくコードユニットの数を数えるため、文字数を知りたい場合はこのようなことをする必要があります。</p>

<pre class="brush: js notranslate">function getCharacterLength (str) {
  // The string iterator that is used here iterates over characters,
  //  not mere code units
  return [...str].length;
}

console.log(getCharacterLength('A\uD87E\uDC04Z')); // 3

// While not recommended, you could add this to each string as follows:

Object.defineProperty(String.prototype, 'charLength', {
  get () {
    return getCharacterLength(this);
  }
});

console.log('A\uD87E\uDC04Z'.charLength); // 3
</pre>

<h2 id="Examples" name="Examples"></h2>

<h3 id="Basic_usage" name="Basic_usage">基本的な使い方</h3>

<pre class="brush: js notranslate">let x = 'Mozilla';
let empty = '';

console.log(x + ' の文字数:' + x.length + ' 文字(コード個数)' );
/* "Mozilla の文字数:7 文字(コード個数)" */

console.log('空文字の文字数:' + empty.length + ' 文字' );
/* "空文字の文字数:0 文字" */</pre>

<h3 id="Assigning_to_length" name="Assigning_to_length">length プロパティへの代入</h3>

<pre class="brush: js notranslate">let myString = "bluebells";

// Attempting to assign a value to a string's .length property has no observable effect.
myString.length = 4;
console.log(myString);
// expected output: "bluebells"
console.log(myString.length);
// expected output: 9
</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-properties-of-string-instances-length')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>

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

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li><a href="https://downloads.teradata.com/blog/jasonstrimpel/2011/11/javascript-string-length-and-internationalizing-web-applications">JavaScript <code>String.length</code> and Internationalizing Web Applications</a></li>
</ul>