๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JAVASCRIPT

[JAVASCRIPT] ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ : indexOf( )

by _ํ† ๋งคํ†  2022. 8. 17.
728x90

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ : indexOf() ๋ฉ”์„œ๋“œ

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜์ธ indexOf() ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜†


#1. indexOf( )

indexOf() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด์—์„œ ์ถ”์ถœํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์ž์˜ ์œ„์น˜๋ฅผ ์ฐพ์€ ํ›„ ์ˆซ์ž๋กœ ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ํŠน์ • ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž์—ด์˜ ์–ด๋Š index์— ์œ„์น˜ํ•˜๋Š”์ง€ ์ฐพ๋Š” ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

! indexOf() ์‚ฌ์šฉ๋ฒ• !
"๋ฌธ์ž์—ด".indexOf(๊ฒ€์ƒ‰๊ฐ’)
"๋ฌธ์ž์—ด".indexOf(๊ฒ€์ƒ‰๊ฐ’, ์œ„์น˜๊ฐ’) : ์ฒ˜์Œ ๊ฐ’์€ ์ถ”์ถœํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์ž์—ด์„ ์ž‘์„ฑํ•˜๊ณ  ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๊ทธ ๋ฌธ์ž์—ด์„ ์ฐพ๊ธฐ ์‹œ์ž‘ ํ•  ์œ„์น˜๋กœ ์ ์œผ๋ฉด ๋œ๋‹ค.

โœ๏ธ์˜ˆ์ œ ์ฝ”๋“œ

const str1 = "javascript reference"
            
const currentStr1 = str1.indexOf("javascript"); // 0
const currentStr2 = str1.indexOf("reference"); // 11 (11๋ฒˆ์งธ ์ž๋ฆฌ : ์œ„์น˜ ๊ฐ’ ์ถœ๋ ฅ)
const currentStr3 = str1.indexOf("j");        // 0
const currentStr4 = str1.indexOf("a");        // 1 (์ค‘๋ณต์ผ ๋•Œ ๊ฐ€์žฅ ์ฒ˜์Œ ๊ฒƒ๋งŒ ์ถœ๋ ฅ)
const currentStr5 = str1.indexOf("v");        // 2
const currentStr6 = str1.indexOf("jquery");   // -1 (์ฐพ๋Š” ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž์—ด์— ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ -1 ์ถœ๋ ฅ)
const currentStr7 = str1.indexOf("b");        // -1

const currentStr8 = str1.indexOf("javascript", 0);   // 0
const currentStr9 = str1.indexOf("javascript", 1);   // -1
const currentStr10 = str1.indexOf("reference", 0);   // 11
const currentStr11 = str1.indexOf("reference", 1);   // 11
const currentStr12 = str1.indexOf("reference", 11);  // 11
const currentStr13 = str1.indexOf("reference", 12);  // -1

! point !
โ€ข ๋งŒ์•ฝ ์„ ํƒํ•œ ๋ฌธ์ž๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
โ€ข ์ฐพ๋Š” ๋ฌธ์ž์—ด์ด ์—†์„ ๊ฒฝ์šฐ ์Œ์ˆ˜์ธ -1์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
โ€ข ๋ฌธ์ž์—ด์„ ์ฐพ์„ ๋•Œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.


#2. lastIndexOf( )

lastIndexOf() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด์—์„œ ์ถ”์ถœํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์ž์˜ ์œ„์น˜๋ฅผ ๋’ค์—์„œ ๋ถ€ํ„ฐ ์ฐพ์€ ํ›„ ์ˆซ์ž๋กœ ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

! lastIndexOf() ์‚ฌ์šฉ๋ฒ• !
"๋ฌธ์ž์—ด".lastIndexOf(๊ฒ€์ƒ‰๊ฐ’)
: indexOf์™€ ๋ฌธ๋ฒ•์€ ๋˜‘๊ฐ™์ง€๋งŒ ๋’ค์—์„œ ๊ฒ€์ƒ‰ํ•œ๋‹ค๋Š” ์ ์ด ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. "๋ฌธ์ž์—ด".lastIndexOf(๊ฒ€์ƒ‰๊ฐ’, ์œ„์น˜๊ฐ’)

โœ๏ธ์˜ˆ์ œ ์ฝ”๋“œ

const str1 = "javascript reference"

const currentStr1 = str1.lastIndexOf("javascript");      // 0
const currentStr2 = str1.lastIndexOf("reference");       // 11
const currentStr3 = str1.lastIndexOf("j");               // 0
const currentStr4 = str1.lastIndexOf("a");               // 3 (์—ฌ๋Ÿฌ๊ฐœ์ผ ๋•Œ ๋งˆ์ง€๋ง‰ a์˜ ์œ„์น˜๋ฅผ ์ถœ๋ ฅ)
const currentStr5 = str1.lastIndexOf("v");               // 2
const currentStr6 = str1.lastIndexOf("jquery");          // -1
const currentStr7 = str1.lastIndexOf("b");               // -1
const currentStr8 = str1.lastIndexOf("javascript", 0);   // 0
const currentStr9 = str1.lastIndexOf("javascript", 1);   // 0
const currentStr10 = str1.lastIndexOf("reference", 0);    // -1
const currentStr11 = str1.lastIndexOf("reference", 1);    // -1
const currentStr12 = str1.lastIndexOf("reference", 11);   // 11
const currentStr13 = str1.lastIndexOf("reference", 12);   // 11

! point !
โ€ข ๋งŒ์•ฝ ์„ ํƒํ•œ ๋ฌธ์ž๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
โ€ข ์ฐพ๋Š” ๋ฌธ์ž์—ด์ด ์—†์„ ๊ฒฝ์šฐ ์Œ์ˆ˜์ธ -1์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
โ€ข ๋ฌธ์ž์—ด์„ ์ฐพ์„ ๋•Œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ˜€

์ž˜ ์ดํ•ดํ•˜์…จ๋‚˜์š”?!
๋‹ค์Œ ์‹œ๊ฐ„์—๋Š” indexOf( ) ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•œ ๊ฒ€์ƒ‰ ์‚ฌ์ดํŠธ๋ฅผ ์‹ค์Šตํ•ด๋ด…์‹œ๋‹ค! ๋ชจ๋‘ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค:)

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css