| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/substring | [Back] [Original] |
Get to know MDN better
const str = "Mozilla";
console.log(str.substring(1, 3));
// : "oz"
console.log(str.substring(2));
// : "zilla"
substring(indexStart)
substring(indexStart, indexEnd)
substring() indexStart indexEnd
indexEnd undefined substring() indexStart indexEnd substring() indexStart indexEnd substring() 2 0 stringName.length 0 stringName.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() 2 start length substring() start end substr() start substring() 0 substr() 0 substring() end start 2 substr() ECMAScript
const text = "Mozilla";
console.log(text.substring(2, 5)); // "zil"
console.log(text.substr(2, 3)); // "zil"
substring() slice()
substring() indexStart indexEnd 2 slice()
const text = "Mozilla";
console.log(text.substring(5, 2)); // "zil"
console.log(text.slice(5, 2)); // ""
NaN substring() 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 "World" "OtherWorld"
function replaceString(oldS, newS, fullS) {
return fullS.split(oldS).join(newS);
}
substring String.prototype.replace()
| ECMAScript 2027 LanguageSpecification # sec-string.prototype.substring |
Stringanchor()at()big()blink()bold()charAt()charCodeAt()codePointAt()concat()endsWith()fixed()fontcolor()fontsize()includes()indexOf()isWellFormed()italics()lastIndexOf()link()localeCompare()match()matchAll()normalize()padEnd()padStart()repeat()replace()replaceAll()search()slice()small()split()startsWith()strike()sub()substr()substring()sup()toLocaleLowerCase()toLocaleUpperCase()toLowerCase()toString()toUpperCase()toWellFormed()trim()trimEnd()trimStart()valueOf()[Symbol.iterator]()Object/Function| Web Proxy Viewer | New URL | Original Page |