[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/substr [Back]  [Original]

String.prototype.substr() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

String.prototype.substr()

Deprecated

Avoid using this feature in new projects. TC39 included substr() in Annex B of the ECMAScript specification, which covers JavaScript features with "one or more undesirable characteristics and in the absence of legacy usage would be removed." This feature may be a candidate for removal from web standards or browsers.

String substr() .

In this article

const str = "Mozilla";

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

console.log(str.substr(2));
// Expected output: "zilla"

js
substr(start)
substr(start, length)

start

.

length Optional

.

.

substr() start length .

  • start >= str.length .
  • start < 0 . max(start + str.length, 0) .
  • start undefined start 0 .
  • length undefined start + length >= str.length substr() .
  • length < 0 .
  • start length NaN 0 .

substr() , substr() substr() slice() substring() . , str = "01234", a = 1, l = -2 str.substr(a, l), str.slice(a, a + l), str.substring(a, a + l) , substr() slice() "123" substring() "0" . a l .

substr()

js
const 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)); // ''

Specification
ECMAScript 2027 LanguageSpecification
# sec-string.prototype.substr


Web Proxy Viewer  |  New URL  |  Original Page