โญ๏ธ ๋ฌธ์์ด ๊ฐ์ฒด ( 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( ) ๋ฉ์๋์ ๋ฆฌํด ----------------------------------------------------------------
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
'Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํจ์์ ์ ํ (2) | 2022.08.23 |
---|---|
๋ฌธ์์ด ๊ฐ์ฒด (9) includes ๋ฉ์๋ (3) | 2022.08.18 |
๋ฌธ์์ด ๊ฐ์ฒด (7) padStart / padEnd ๋ฉ์๋ (2) | 2022.08.18 |
๋ฌธ์์ด ๊ฐ์ฒด (6) concat / repeat ๋ฉ์๋ (2) | 2022.08.18 |
๋ฌธ์์ด ๊ฐ์ฒด (5) replace / replaceAll ๋ฉ์๋ (2) | 2022.08.18 |
๋๊ธ