| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/substring | [Back] [Original] |
Get to know MDN better
const str = "Mozilla";
console.log(str.substring(1, 3));
// Expected output: "oz"
console.log(str.substring(2));
// Expected output: "zilla"
substring(indexStart)
substring(indexStart, indexEnd)
substring() indexStart indexEnd
indexEnd substring() indexStart indexEnd substring() indexStart indexEnd substring() 0 str.length 0 str.length
NaN 0
substring "Mozilla"
const anyString = "Mozilla";
console.log(anyString.substring(0, 1)); // 'M'
console.log(anyString.substring(1, 0)); // 'M'
console.log(anyString.substring(0, 6)); // 'Mozill'
console.log(anyString.substring(4)); // 'lla'
console.log(anyString.substring(4, 7)); // 'lla'
console.log(anyString.substring(7, 4)); // 'lla'
console.log(anyString.substring(0, 7)); // 'Mozilla'
console.log(anyString.substring(0, 10)); // 'Mozilla'
substring() length
const text = "Mozilla";
// 4
console.log(text.substring(text.length - 4)); // illa
// 5
console.log(text.substring(text.length - 5)); // zilla
substring() substr()
substr() start length substring() start endsubstr() start substring() 0substr() substring() end start substr() ECMAScript
const text = "Mozilla";
console.log(text.substring(2, 5)); // "zil"
console.log(text.substr(2, 3)); // "zil"
substring() slice()
substring() indexStart indexEnd slice()
const text = "Mozilla";
console.log(text.substring(5, 2)); // "zil"
console.log(text.slice(5, 2)); // ""
NaNsubstring() 0
console.log(text.substring(-5, 2)); // "Mo"
console.log(text.substring(-5, -2)); // ""
slice() NaN 0
console.log(text.slice(-5, 2)); // ""
console.log(text.slice(-5, -2)); // "zil"
Brave New World Brave New Web
// fullS oldS newS
function replaceString(oldS, newS, fullS) {
for (let i = 0; i < fullS.length; ++i) {
if (fullS.substring(i, i + oldS.length) === oldS) {
fullS =
fullS.substring(0, i) +
newS +
fullS.substring(i + oldS.length, fullS.length);
}
}
return fullS;
}
replaceString("World", "Web", "Brave New World");
oldS newS "OtherWorld" "World"
function replaceString(oldS, newS, fullS) {
return fullS.split(oldS).join(newS);
}
| ECMAScript 2027 LanguageSpecification # sec-string.prototype.substring |
StringString.prototype.anchor()String.prototype.at()String.prototype.big()String.prototype.blink()String.prototype.bold()String.prototype.charAt()String.prototype.charCodeAt()String.prototype.codePointAt()String.prototype.concat()String.prototype.endsWith()String.prototype.fixed()String.prototype.fontcolor()String.prototype.fontsize()String.prototype.includes()String.prototype.indexOf()String.prototype.isWellFormed()String.prototype.italics()String.prototype.lastIndexOf()String.prototype.link()String.prototype.localeCompare()String.prototype.match()String.prototype.matchAll()String.prototype.normalize()String.prototype.padEnd()String.prototype.padStart()String.prototype.repeat()String.prototype.replace()String.prototype.replaceAll()String.prototype.search()String.prototype.slice()String.prototype.small()String.prototype.split()String.prototype.startsWith()String.prototype.strike()String.prototype.sub()String.prototype.substr()String.prototype.substring()String.prototype.sup()String.prototype.toLocaleLowerCase()String.prototype.toLocaleUpperCase()String.prototype.toLowerCase()String.prototype.toString()String.prototype.toUpperCase()String.prototype.toWellFormed()String.prototype.trim()String.prototype.trimEnd()String.prototype.trimStart()String.prototype.valueOf()String.prototype[Symbol.iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()| Web Proxy Viewer | New URL | Original Page |