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

๋ฌธ์ž์—ด ๊ฐ์ฒด (8) indexOf / lastindexOf ๋ฉ”์„œ๋“œ

by oranssy 2022. 8. 18.
728x90
๋ฐ˜์‘ํ˜•

โญ๏ธ ๋ฌธ์ž์—ด ๊ฐ์ฒด ( indexOf / lastindexOf ๋ฉ”์„œ๋“œ ) โญ๏ธ


01. ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด์˜ ์ข…๋ฅ˜ : ๋ฌธ์ž์—ด ๊ฐ์ฒด

ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด(Standard Built-in Object)๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋“ค์„ ๋งํ•˜๋ฉฐ, ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ๊ธฐ์ดˆ๊ฐ€ ๋˜๋Š” ํ•ต์‹ฌ์ ์ธ ๊ฐ์ฒด ์ž…๋‹ˆ๋‹ค.
๋‚ด์žฅ ๊ฐ์ฒด์˜ ์ข…๋ฅ˜์—๋Š” Object, Fuction, String, Array, Math, Number, Event, Boolean, Data, RegExp ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ ์ค‘์—์„œ ๋ฌธ์ž์—ด ๊ฐ์ฒด(String Object)๋Š” ๋ฌธ์ž์—ด์„ ์ €์žฅ/๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

   ___ ์ฐธ๊ณ ํ•˜๊ธฐ ___
  `1   ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•  ๋•Œ๋Š” ''"" ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, new ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.
  `2   ๋ฌธ์ž์—ด ๊ฐ์ฒด๋Š” ๋ถˆ๋ณ€์„ฑ์ด ์žˆ์œผ๋ฉฐ, ์ด๋กœ์จ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์˜ ์ ˆ์•ฝ๊ณผ ๋ณด์•ˆ์„ฑ ๋ฐ ๋™์‹œ์„ฑ์˜ ํŠน์ง•๋„ ํ•จ๊ป˜ ๊ฐ–์Šต๋‹ˆ๋‹ค.
      

02. indexOf / lastindexOf ๋ฉ”์„œ๋“œ

๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์˜ ์œ„์น˜๋ฅผ ์ฐพ๊ณ , ์ฐพ์€ ์œ„์น˜์˜ ์ฒซ๋ฒˆ์งธ index ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
๐Ÿข indexOf( ) : ํŠน์ • ๋ฌธ์ž์˜ ์œ„์น˜๋ฅผ ์•ž์—์„œ๋ถ€ํ„ฐ ์ฐพ๊ณ , ์ฐพ์€ ์œ„์น˜์˜ ์ฒซ๋ฒˆ์งธ index ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
๐Ÿข lastIndexOf( ) : ํŠน์ • ๋ฌธ์ž์˜ ์œ„์น˜๋ฅผ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฐพ๊ณ , ์ฐพ์€ ์œ„์น˜์˜ ์ฒซ๋ฒˆ์งธ index ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
์‹œ์ž‘ ์œ„์น˜๊ฐ’์„ ์„ค์ •ํ•˜๋ฉด, ์•ž/๋’ค์˜ ์‹œ์ž‘๋˜๋Š” ์œ„์น˜๋ถ€ํ„ฐ ์ž˜๋ ค์ง„ ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์—†์œผ๋ฉด -1 ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

[1] indexOf( ) ์™€ lastIndexOf( ) ๋ฉ”์„œ๋“œ์˜ ํ˜•์‹

"๋ฌธ์ž์—ด".indexOf(๊ฒ€์ƒ‰๊ฐ’);
"๋ฌธ์ž์—ด".indexOf(๊ฒ€์ƒ‰๊ฐ’, ์‹œ์ž‘ ์œ„์น˜๊ฐ’);
// indexOf( ) ๋ฉ”์„œ๋“œ์˜ ๋ฆฌํ„ด ----------------------------------------------------------------
    const str1 = "javascript reference";

    const currentStr1 = str1.indexOf("javascript");  //  0
    const currentStr2 = str1.indexOf("reference");  //  11  //  r ์˜ index ๊ฐ’ 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    ์œ„์น˜์— ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์–ด์„œ -1

// lastIndexOf( ) ๋ฉ”์„œ๋“œ์˜ ๋ฆฌํ„ด ----------------------------------------------------------------
    const str1 = "javascript reference";

    const currentStr14 = str1.lastIndexOf("javascript");  //  0
    const currentStr15 = str1.lastIndexOf("reference");  //  11
    const currentStr16 = str1.lastIndexOf("j");  //  0
    const currentStr17 = str1.lastIndexOf("a");  //  3
    const currentStr18 = str1.lastIndexOf("r");  //  15
    const currentStr19 = str1.lastIndexOf("e");  //  19
    const currentStr20 = str1.lastIndexOf("b");  //  -1   
    const currentStr21 = str1.lastIndexOf("jquery");  //  -1    ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฏ€๋กœ -1
    const currentStr22 = str1.lastIndexOf("javascript", 0);  //  0
    const currentStr23 = str1.lastIndexOf("javascript", 1);  //  0
    const currentStr24 = str1.lastIndexOf("reference", 0);  //  -1
    const currentStr25 = str1.lastIndexOf("reference", 1);  //  -1
    const currentStr26 = str1.lastIndexOf("reference", 11);  //  11
    const currentStr27 = str1.lastIndexOf("reference", 12);  //  11
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€