diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
| commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
| tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/ja/web/javascript/reference/global_objects/string/length | |
| parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
| download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip | |
initial commit
Diffstat (limited to 'files/ja/web/javascript/reference/global_objects/string/length')
| -rw-r--r-- | files/ja/web/javascript/reference/global_objects/string/length/index.html | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/global_objects/string/length/index.html b/files/ja/web/javascript/reference/global_objects/string/length/index.html new file mode 100644 index 0000000000..62c3016f34 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/string/length/index.html @@ -0,0 +1,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> |
