--- title: String.prototype.substr() slug: Web/JavaScript/Reference/Global_Objects/String/substr tags: - Deprecated - JavaScript - Method - Prototype - Reference - String - メソッド translation_of: Web/JavaScript/Reference/Global_Objects/String/substr --- <div>{{JSRef}}</div> <p class="seoSummary"><strong><code>substr()</code></strong> メソッドは、文字列の一部を、指定した位置から後方向指定した文字数だけ返します。</p> <div>{{EmbedInteractiveExample("pages/js/string-substr.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>.substr(<var>start</var>[, <var>length</var>])</pre> <h3 id="Parameters" name="Parameters">引数</h3> <dl> <dt><code><var>start</var></code></dt> <dd>返却する部分文字列に含まれる最初の文字の位置です。</dd> <dt><code><var>length</var></code></dt> <dd>任意です。取り出す文字の数です。</dd> </dl> <h3 id="Return_value" name="Return_value">返値</h3> <p>指定された文字列の指定された部分が入った新しい文字列です。</p> <h2 id="Description" name="Description">解説</h2> <p><code>substr()</code> は、 <code><var>str</var></code> のうち <code><var>length</var></code> 文字分を、 <code><var>start</var></code> の位置から数えて抽出します。</p> <ul> <li><code><var>start</var></code> が正の数である場合、文字列の先頭から数えた位置になります。この値は <code><var>str</var>.length</code> が上限になります。</li> <li><code><var>start</var></code> が負の数である場合、文字列の末尾から数えた位置になります。この値は <code>-<var>str</var>.length</code> が下限になります。</li> <li>注: Microsoft の JScript では、 <code><var>start</var></code> の引数が負の数であっても文字列の末尾からの位置にはなりません。</li> <li><code><var>length</var></code> が省略された場合、 <code>substr()</code> は文字列の末尾までの文字を抽出します。</li> <li><code><var>length</var></code> が {{jsxref("undefined")}} である場合、 <code>substr()</code> は文字列の末尾までの文字を抽出します。</li> <li><code><var>length</var></code> が負の数である場合、 <code>0</code> として扱われます。</li> <li><code><var>start</var></code> および <code><var>length</var></code> において、 {{jsxref("NaN")}} は <code>0</code> として扱われます。</li> </ul> <h2 id="Polyfill" name="Polyfill">ポリフィル</h2> <p>Microsoft の JScript は start の位置として負の数に対応していません。この機能を使用したい場合は、このバグを回避するために、次の互換コードを使用することができます。</p> <pre class="brush: js notranslate">// only run when the substr() function is broken if ('ab'.substr(-1) != 'b') { /** * Get the substring of a string * @param {integer} start where to start the substring * @param {integer} length how many characters to return * @return {string} */ String.prototype.substr = function(substr) { return function(start, length) { // call the original method return substr.call(this, // did we get a negative start, calculate how much it is from the beginning of the string // adjust the start parameter for negative value start < 0 ? this.length + start : start, length) } }(String.prototype.substr); } </pre> <h2 id="Examples" name="Examples">例</h2> <h3 id="Using_substr" name="Using_substr">substr() の使用</h3> <pre class="brush: js notranslate">var aString = 'Mozilla'; console.log(aString.substr(0, 1)); // 'M' console.log(aString.substr(1, 0)); // '' console.log(aString.substr(-1, 1)); // 'a' console.log(aString.substr(1, -1)); // '' console.log(aString.substr(-3)); // 'lla' console.log(aString.substr(1)); // 'ozilla' console.log(aString.substr(-20, 2)); // 'Mo' console.log(aString.substr(20, 2)); // '' </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.substr', 'String.prototype.substr')}}</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.substr")}}</p> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li>{{jsxref("String.prototype.slice()")}}</li> <li>{{jsxref("String.prototype.substring()")}}</li> </ul>