Animated Rainbow Nyan Cat
본문 바로가기
javascript

slice()/substring()/substr()

by 이유나1 2022. 8. 16.
728x90
반응형

slice()

// "문자열", slice(시작위치)
// "문자열", slice(시작위치, 끝나는 위치)
const str1 = "javascript relerence"
const currentStr1 = str1.slice(0); //javascript relerence
const currentStr2 = str1.slice(1); //javascript relerence
const currentStr3 = str1.slice(2); //vescript relerence
const currentStr4 = str1.slice(0, 1); //1
const currentStr5 = str1.slice(0, 2); //ja
const currentStr6 = str1.slice(0, 3); //jav
const currentStr7 = str1.slice(1, 2); //a
const currentStr8 = str1.slice(1, 3); //av
const currentStr9 = str1.slice(1, 4); //avs
const currentStr10 = str1.slice(-1); //e
const currentStr11 = str1.slice(-2); //ec
const currentStr12 = str1.slice(-3); //nce
const currentStr13 = str1.slice(-3, -1); //nc
const currentStr14 = str1.slice(-3, -2); //n
const currentStr15 = str1.slice(-3, -3); //안나온다.

substring()

// 시작위치 값은 끝나는 위치 값보다 작아야 합니다. 시작위치 &lt 끝나는 위치
// substring() 시작값이 끝나는 값보가 큰 경우 두 값을 바꿔서 처리(에러 방지)
const currentStr16 = str1.slice(1, 4); //ava
const currentStr17 = str1.slice(4, 1); //'' 에러방지?
const currentStr18 = str1.substring(1, 4); //ava
const currentStr19 = str1.substring(4, 1); //ava  substring 알아서 (숫자) 바꿔준다.

substr()

// "문자열", substr(시작위치)
// "문자열", substr(시작위치,길이)
const currentStr20 = str1.substr(0); //javascript relerence
const currentStr21 = str1.substr(1); //avastript relerence
const currentStr22 = str1.substr(2); //vascript relerence
const currentStr23 = str1.substr(0, 1); //j
const currentStr24 = str1.substr(0, 2); //ja
const currentStr25 = str1.substr(0, 3); //jav
const currentStr26 = str1.substr(1, 2); //av
const currentStr27 = str1.substr(1, 3); //ava
const currentStr28 = str1.substr(1, 4); //avas
const currentStr29 = str1.substr(-1); //e
const currentStr30 = str1.substr(-2); //ce
const currentStr31 = str1.substr(-3); //nce
const currentStr32 = str1.substr(-1, 1); //e
const currentStr33 = str1.substr(-2, 2); //ce
const currentStr34 = str1.substr(-3, 3); //nce
728x90

'javascript' 카테고리의 다른 글

문자열 결합 / 템플릿 문자열  (2) 2022.08.17
indexOf() / lastIndexOf  (3) 2022.08.16
정규식 표현  (3) 2022.08.16
내장함수  (1) 2022.08.14
join() / push() / pop()  (2) 2022.08.11

댓글


/
/
/

CSS
광고준비중입니다.