Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. String
  6. substr()

String.prototype.substr()

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see thecompatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Thesubstr() method ofString values returns a portion of this string, starting at the specified index and extending for a given number of characters afterwards.

Note:substr() is not part of the main ECMAScript specification — it's defined inAnnex B: Additional ECMAScript Features for Web Browsers, which is normative optional for non-browser runtimes. Therefore, people are advised to use the standardString.prototype.substring() andString.prototype.slice() methods instead to make their code maximally cross-platform friendly. TheString.prototype.substring() page has some comparisons between the three methods.

Try it

const str = "Mozilla";console.log(str.substr(1, 2));// Expected output: "oz"console.log(str.substr(2));// Expected output: "zilla"

Syntax

js
substr(start)substr(start, length)

Parameters

start

The index of the first character to include in the returned substring.

lengthOptional

The number of characters to extract.

Return value

A new string containing the specified part of the given string.

Description

A string'ssubstr() method extractslength characters from the string, counting from thestart index.

  • Ifstart >= str.length, an empty string is returned.
  • Ifstart < 0, the index starts counting from the end of the string. More formally, in this case the substring starts atmax(start + str.length, 0).
  • Ifstart is omitted orundefined, it's treated as0.
  • Iflength is omitted orundefined, or ifstart + length >= str.length,substr() extracts characters to the end of the string.
  • Iflength < 0, an empty string is returned.
  • For bothstart andlength,NaN is treated as0.

Although you are encouraged to avoid usingsubstr(), there is no trivial way to migratesubstr() to eitherslice() orsubstring() in legacy code without essentially writing a polyfill forsubstr(). For example,str.substr(a, l),str.slice(a, a + l), andstr.substring(a, a + l) all have different results whenstr = "01234", a = 1, l = -2substr() returns an empty string,slice() returns"123", whilesubstring() returns"0". The actual refactoring path depends on the knowledge of the range ofa andl.

Examples

Using substr()

js
const string = "Mozilla";console.log(string.substr(0, 1)); // 'M'console.log(string.substr(1, 0)); // ''console.log(string.substr(-1, 1)); // 'a'console.log(string.substr(1, -1)); // ''console.log(string.substr(-3)); // 'lla'console.log(string.substr(1)); // 'ozilla'console.log(string.substr(-20, 2)); // 'Mo'console.log(string.substr(20, 2)); // ''

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.substr

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp