๐Ÿ“
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • ์ฝœ๋ฐฑํ•จ์ˆ˜ (Callback function)
    • ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ
    • ์ž๋ฃŒ๊ตฌ์กฐ new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • ๊ฐ์ฒด
    • Arguments
    • higher order function
    • operators-function-control-flow
    • ๊ฐ์ฒด ์ƒ์„ฑ ํŒจํ„ด 3๊ฐ€์ง€
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • ๋ฐ์ดํ„ฐ ํƒ€์ž…
    • Object ๊ฐ์ฒด
    • ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด์˜ ํ™•์žฅ
    • ์ฐธ์กฐ
    • ํ•จ์ˆ˜
    • ์ƒ์†(Inheritance)
    • this - 'this'๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ '์–ด๋–ป๊ฒŒ' ์‹คํ–‰ํ•˜๋А๋ƒ์— ๋”ฐ๋ฅธ 4๊ฐ€์ง€ this ์ •์˜
    • ์ „์—ญ๊ฐ์ฒด(Global object)
    • ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - ์ž์—ฐ์ˆ˜, ์ •์ˆ˜, 10์ง„์ˆ˜, 2์ง„์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜์ , ์‹ค์ˆ˜
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šค, ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์กด์žฌํ• ๊นŒ?
    • Hooks๊ณผ useEffect ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
    • Route
    • async wait ์‚ฌ์šฉํ•˜๊ธฐ
    • Hooks API Reference
    • context
    • npm uninstall ํ•˜๋Š”๋ฒ•
    • test ๋งŒ๋“ค๊ธฐ
  • tip
    • ํด๋ฆฐ์ฝ”๋“œ
    • BxSlider๋กœ ํ…์ŠคํŠธ ํ๋ฅด๋Š” ํšจ๊ณผ ๋งŒ๋“ค๊ธฐ
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ 2๋ถ€
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • ๋จธ์ง€ ํ›„ branch ์‚ญ์ œํ•˜๊ธฐ
    • ์ง€์˜ฅ์—์„œ ์˜จ Git
    • reset-revert
    • develop branch ๋ฅผ pull ํ•˜๊ณ  ์‹ถ์„๋•Œ
  • conference-and-seminar
    • ๋ชจ๋˜ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœํ™˜๊ฒฝ์˜ ์ดํ•ด
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋“œ๋ฆฌ๋ทฐ
    • ์Šค์Šค๋กœ ๊ณต๋ถ€ํ•˜๋Š” ๋ฒ•
    • ๋ฐ”๋‹๋ผ์ฝ”๋”ฉ ์ˆ˜๊ฐ• ํ›„๊ธฐ
    • async ๊ณผ์ œ ํ›„๊ธฐ - ๋น„๋™๊ธฐ, ๋™๊ธฐ, ํด๋กœ์ €, ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด
    • ์œ ์šฉํ•œ ์‚ฌ์ดํŠธ
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars ์ด์šฉ์ž ์†”๋ฃจ์…˜ ๋ชจ์Œ
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • ํƒ€์ž…๊ณผ ์ถ”์ƒํ™”
      • ๊ฐ์ฒด ์ง€๋„
      • ์ด์ƒํ•œ ๋‚˜๋ผ์˜ ๊ฐ์ฒด
      • ์ถ”์ƒํ™” ๊ธฐ๋ฒ•
      • 05. ์ฑ…์ž„๊ณผ ๋ฉ”์‹œ์ง€
      • 07.ํ•จ๊ป˜ ๋ชจ์œผ๊ธฐ
      • 04. ์—ญํ• , ์ฑ…์ž„, ํ˜‘๋ ฅ
      • ํ˜‘๋ ฅํ•˜๋Š” ๊ฐ์ฒด๋“ค์˜ ๊ณต๋™์ฒด
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • ์›น ๋ธŒ๋ผ์šฐ์ €์— URL์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์›น ํŽ˜์ด์ง€๊ฐ€ ๋ณด์—ฌ์งˆ๊นŒ?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex ํ•ด๋ฒ„๋ ธ์ง€ ๋ญ์•ผ
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd ๋ช…๋ น์–ด ๋ชจ์Œ
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN ์„ค์ •ํ•˜๊ธฐ
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans ์˜ค๋‹ต๋…ธํŠธ
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 2/2
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 1/2
  • you-dont-know-js
    • ํƒ€์ž…
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • Syntax
  • Parameters
  • Return value
  • Description
  • Examples
  • substring() ์‚ฌ์šฉํ•˜๊ธฐ
  • length ์†์„ฑ๊ณผ ํ•จ๊ป˜ substring() ์‚ฌ์šฉํ•˜๊ธฐ
  • substring()๊ณผ substr()์˜ ์ฐจ์ด
  • substring()๊ณผ slice()์˜ ์ฐจ์ด
  • ๋ฌธ์ž์—ด ๋‚ด์—์„œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๋Œ€์ฒด

Was this helpful?

  1. javascript-api

string.substring

substring() ๋ฉ”์„œ๋“œ๋Š” string ์˜ ์‹œ์ž‘ index์™€ ์ข…๋ฃŒ index ์ „๊นŒ์ง€ ํ˜น์€ ๋ฌธ์ž์—ด ๋ ๋ถ€๋ถ„์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const str = 'Cats are the best';

str.substring(1, 3); // "at"
str.substring(2); // "ts are the best"

Syntax

str.substring(indexStart[, indexEnd])

Parameters

indexStart

๋ฐ˜ํ™˜๋œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์— ํฌํ•จํ•  ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž์˜ index

indexEnd (Optional)

๋ฐ˜ํ™˜๋œ ๋ถ€๋ถ„์˜ ๋ฌธ์ž์—ด์—์„œ ์ œ์™ธํ•  ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž์˜ index

Return value

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ์ง€์ •๋œ ๋ถ€๋ถ„์„ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด

Description

substring() ๋ฉ”์„œ๋“œ๋Š” indexStart๋ถ€ํ„ฐ ์ถ”์ถœํ•˜์ง€๋งŒ indexEnd๋Š” optional์ด๋ผ ํฌํ•จํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

  • ๋งŒ์•ฝ indexEnd ๊ฐ€ ์ƒ๋žต๋œ ๊ฒฝ์šฐ, substring() ๋ฌธ์ž์—ด์˜ ๋๊นŒ์ง€ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • ๋งŒ์•ฝ indexStart๊ฐ€ indexEnd ์™€ ๊ฐ™์„ ๊ฒฝ์šฐ, substring() ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • ๋งŒ์•ฝ indexStart ๊ฐ€ indexEnd๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ, substring() ๋ฉ”์„œ๋“œ๋Š” ๋งˆ์น˜ ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ”๊พผ ๋“ฏ ์ž‘๋™ํ•œ๋‹ค.

0๋ณด๋‹ค ์ž‘์€ ์ธ์ž๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ์—๋Š” 0์œผ๋กœ,

stringName.length๋ณด๋‹ค ํฐ ์ธ์ž ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ, stringName.length๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.

NaN ๊ฐ’์€ 0์œผ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.

Examples

substring() ์‚ฌ์šฉํ•˜๊ธฐ

const anyString = 'Mozilla';

// M์„ ๋ฝ‘์•„๋ณด์ž!
anyString.substring(0, 1); // "M"
anyString.substring(1, 0); // "M" ์›ƒ๊ธฐ๋„น indexStart > indexEnd ๋•Œ๋ฌธ์— ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ”๊พผ ๋“ฏ ์ž‘๋™ํ•œ๋‹ค.


// Mozill ๋ฝ‘๊ธฐ
anyString.substring(0, 6); // "Mozill"
anyString.substring(6, 0); // "Mozill"

// lla ๋ฝ‘๊ธฐ
anyString.substring(4); // "lla"
anyString.substring(4, 7); // "lla"
anyString.substring(7, 4); // "lla"

// Mozilla ๋ฝ‘๊ธฐ
anyString.substring(0); // "Mozilla"
anyString.substring(0, 7); // "Mozilla"
anyString.substring(0, 10); // "Mozilla"

length ์†์„ฑ๊ณผ ํ•จ๊ป˜ substring() ์‚ฌ์šฉํ•˜๊ธฐ

substring()๋ฉ”์„œ๋“œ์™€ length์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ถ”์ถœํ•œ๋‹ค. ์‹œ์ž‘ ๋ฐ ๋ index๋ฅผ ์•Œ ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ฐฉ๋ฒ•์ด ๋” ํŽธํ•  ์ˆ˜ ์žˆ๋‹ค.

// ๋งˆ์ง€๋ง‰ 4๊ธ€์ž์ธ illa ์ถ”์ถœํ•˜๊ธฐ
let anyString = 'Mozilla';
anyString.substring(anyString.length - 4); // "illa"

// ๋งˆ์ง€๋ง‰ 5๊ธ€์ž์ธ zilla ์ถ”์ถœํ•˜๊ธฐ
anyString.substring(anyString.length - 5); // "zilla"

substring()๊ณผ substr()์˜ ์ฐจ์ด

substring()๊ณผ substr() ๋ฉ”์„œ๋“œ ์‚ฌ์ด์—๋Š” ๋ฏธ๋ฌ˜ํ•œ ์ฐจ์ด๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ํ˜ผ๋™ํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค.

substring() ์˜ index๋Š” ์‹œ์ž‘์œ„์น˜์™€ ์ข…๋ฃŒ ์œ„์น˜๋ฅผ ์ธ๋ฑ์Šค๋กœ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ, substr()์ธ์ˆ˜๋Š” ์‹œ์ž‘ ์œ„์น˜์™€ ๋ฌธ์ž์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

๋˜ํ•œ ECMAScript์—์„œ substr() ์€ ๋ ˆ๊ฑฐ์‹œ ๊ธฐ๋Šฅ์œผ๋กœ ๊ฐ„์ฃผ๋˜์–ด ์ดํ›„ ๋ฒ„์ „์—์„œ ์ œ๊ฑฐ ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ฐ€๋Šฅํ•˜๋ฉด ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

const text = 'Mozilla';
text.substring(2, 5); // 'zil'
text.substr(2, 3); // 'zil'

substring()๊ณผ slice()์˜ ์ฐจ์ด

substring()๊ณผ slice()๋Š” ๋ฐฉ๋ฒ•์€ ๊ฑฐ์˜ ๋™์ผํ•˜์ง€๋งŒ, ์Œ์ˆ˜ index์˜ ์ทจ๊ธ‰์— ๋Œ€ํ•ด ๋‘๊ฐ€์ง€ ๋ฏธ๋ฌ˜ํ•œ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

indexStart > indexEnd ์ผ ๊ฒฝ์šฐ substring()์€ ๋‘ ๊ฐœ์˜ index๋ฅผ ์Šค์™‘ํ•˜๊ณ , slice()๋Š” ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const text = 'Mozilla';
text.substring(5, 2); // "zil"
text.slice(5, 2); // ""

์ธ์ž ํ•˜๋‚˜ ํ˜น์€ ๋ชจ๋‘๊ฐ€ ์Œ์ˆ˜ ๋˜๋Š” NaN์ธ ๊ฒฝ์šฐ, substring() ๋ฉ”์„œ๋“œ๋Š” ์ด๋ฅผ 0์œผ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.

text.substring(-5, 2); // "Mo"
text.substring(-5, -2); // ""

slice() ๋Š” ์ธ์ž๊ฐ€ NaN ์ธ ๊ฒฝ์šฐ 0์œผ๋กœ ์ทจ๊ธ‰ํ•˜์ง€๋งŒ, ์Œ์ˆ˜์ธ ๊ฒฝ์šฐ index๋ฅผ ๋’ค์—์„œ๋ถ€ํ„ฐ ๊ฑฐ๊พธ๋กœ ์„ธ์–ด ์ฐพ๋Š”๋‹ค.

๋งŒ์•ฝ ์ธ์ž๊ฐ€ -5๋ผ๋ฉด string.length - 5 ๋กœ ํ•ด์„œ ์ฐพ๋Š”๋‹ค.

text.slice(-5, 2); // 7(text.length) - 5 text.slice(2, 2) => ''
text.slice(-5, -2); // text.slice(2, 5) => "zil"

๋ฌธ์ž์—ด ๋‚ด์—์„œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๋Œ€์ฒด

๋‹ค์Œ ์˜ˆ์ œ๋Š” ๋ฌธ์ž์—ด์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ”๊พผ๋‹ค. ๋‹จ๋… ๋ฌธ์ž์™€ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ๋ฐ”๊พผ๋‹ค.

'Brave New Word'๋ผ๋Š” ๋ฌธ์ž์—ด์„ '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'); // "Brave New web"

oldS ์ž์ฒด๊ฐ€ newS์˜ ํ•˜์œ„ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ (์˜ˆ: ์—ฌ๊ธฐ์„œ 'World'๋ฅผ 'otherWorld'๋กœ ๋Œ€์ฒดํ•˜๋ ค๊ณ  ์‹œ๋„ ํ–ˆ์„ ๊ฒฝ์šฐ) ๋ฌดํ•œ ๋ฃจํ”„๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

์•„ํ•˜..for๋ฌธ์ค‘์— fullS๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ .. ใ…Ž.. ๋‹ค์‹œ ํ…์ŠคํŠธ๊ฐ€ ๊ต์ฒด๋˜๊ณ ..ใ…Ž.. ๋˜ for๋ฌธ์ด ๋Œ์•„๊ฐ€๊ณ ..ใ…Ž text๊ฐ€ ๊ต์ฒด๋˜๊ณ  ใ…Žใ…Ž length๋Š” ๊ณ„์† ๊ธธ์–ด์ง€๊ณ  ใ…Žใ…Žใ…Ž

function replaceString(oldS, newS, fullS) {
  return fullS.split(oldS).join(newS);
}

replaceString('World', 'otherWorld', 'Brave New World'); // "Brave New otherWorld"

๋ฌด์กฐ๊ฑด 2๊ฐœ๋กœ ๋‚˜๋ˆ ์ง€๊ณ  ๊ทธ ์‚ฌ์ด์— newS๊ฐ€ ๋“ค์–ด๊ฐ€์„œ join๋˜๊ธฐ ๋•Œ๋ฌธ์—! ๋ฐ”๋€Œ๋Š” ๋ฌธ์ž์—ด์ด ์–ด๋””์— ์œ„์น˜ํ•˜๋˜ ์ƒ๊ด€ ์—†๋‹ค. ๐Ÿ˜ฎ

ํ•˜์ง€๋งŒ ๋Œ€๋ถ€๋ถ„ replace๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

Previousstring.padEndNextstring.length

Last updated 5 years ago

Was this helpful?