๐ก ๋์ ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํต์ฌ ๊ฐ์ด๋๋ฅผ ์ฝ๊ณ ์ ๋ฆฌํฉ๋๋ค.
5.1 ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์์ด ๋ฉ์๋
indexOf()
Gets the position of the first occurrence of the specified value in a string.
๋ฌธ์์ด์์ ์ง์ ๋ ๊ฐ์ด ์ฒ์ ๋ํ๋๋ ์์น๋ฅผ ๋ฐํ
const str = "this is a short sentence";
str.indexOf("short");
// Output: 10
ํ์ฉ ์์
// ํ์ผ์ ํ์ฅ์ ํน์ ์ ๊ท์ ๊ฒ์ฌ ๋ฑ์ ๋ง์ด ์ฌ์ฉ๋๋ค.
if ( file.indexOf("xlsx") > -1 ) {
// ...
}
slice()
Pulls a specified part of a string as a new string.
๋ฌธ์์ด์ ์ง์ ๋ ๋ถ๋ถ์ ์ ๋ฌธ์์ด๋ก ๋ฐํ (์๋ณธ์ ๋ณํ์ง ์์)
const str = "pizza, orange, cereals"
str.slice(0, 5);
// Output: "pizza"
toUpperCase()
Turns all characters of a string to uppercase.
๋ฌธ์์ด ๋ด์ ๋ชจ๋ ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๋ฐ๊พผ๋ค.
const str = "i ate an apple"
str.toUpperCase()
// Output: "I ATE AN APPLE"
toLowerCase()
Turns all characters of a string to lowercase.
๋ฌธ์์ด ๋ด์ ๋ชจ๋ ๋ฌธ์๋ฅผ ์๋ฌธ์๋ก ๋ฐ๊พผ๋ค.
const str = "I ATE AN APPLE"
str.toLowerCase()
// Output: "i ate an apple"
5.2 ์๋ก์ด ๋ฌธ์์ด ๋ฉ์๋
๋ค์ ๋ค ๊ฐ์ง ๋ฉ์๋๋ ES6์์ ์๋ก ๋์ ๋ ๋ฉ์๋์ด๋ค
startsWith()
endsWith()
includes()
repeat()
startsWith()
This new method will check if the string starts with the value we pass in:
๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ๊ฐ์ผ๋ก ๋ฌธ์์ด์ด ์์ํ๋์ง ํ์ธํ์ฌ boolean ๊ฐ์ ๋ฐํํ๋ค.
const code = "ABCDEFG";
code.startsWith("ABB");
// false
code.startsWith("abc");
// false, startsWith is case sensitive
code.startsWith("ABC");
// true
๋งค๊ฐ๋ณ์๋ฅผ ์ถ๊ฐ๋ก ์ ๋ฌํ์ฌ ๋ฉ์๋๊ฐ ๊ฒ์ฌ๋ฅผ ์์ํ๋ ์์์ ์ ์ง์ ํ ์๋ ์๋ค.
const code = "ABCDEFGHI"
code.startsWith("DEF",3);
// true, 3๊ฐ ๋ฌธ์๋ฅผ ์ง๋์ ๊ณ์ฐ
endsWith()
Similarly to startsWith()
, this new method will check if the string ends with the value we pass in:
startsWith()
์ ์ ์ฌํ๊ฒ ๋ฌธ์์ด์ด ์ฐ๋ฆฌ๊ฐ ์ ๋ฌํ ๊ฐ์ผ๋ก ๋๋๋์ง ํ์ธํ๋ค.
const code = "ABCDEF";
code.endsWith("DDD");
// false
code.endsWith("def");
// false, endsWith is case sensitive
code.endsWith("DEF");
// true
๋งค๊ฐ๋ณ์๋ฅผ ์ ๋ฌํ์ฌ ๊ฒ์ฌํ ๋ฌธ์์ด์ ๋ฒ์๋ฅผ ์ ๋ฌํ ์ ์๋ค.
const code = "ABCDEFGHI"
code.endsWith("EF", 6);
// true, ์ฒซ 6๊ฐ ๋ฌธ์์ธ ABCDEF๋ง์ ๊ณ ๋ ค
includes()
This method will check if our string includes the value we pass in.
์ฐ๋ฆฌ๊ฐ ์ ๋ฌํ ๊ฐ์ด ๋ฌธ์์ด์ ํฌํจ๋์ด ์๋์ง ํ์ธํ๋ค.
const code = "ABCDEF"
code.includes("ABB");
// false
code.includes("abc");
// false, includes is case sensitive
code.includes("CDE");
// true
repeat()
This new method will take an argument that specifies the number of times it needs to repeat the string
.
ํ์๋ฅผ ์ธ์๋ก ๋ฐ์ ๋ฌธ์์ด์ ํ์๋งํผ ๋ฐ๋ณตํ๋ค.
let hello = "Hi";
console.log(hello.repeat(10));
// "HiHiHiHiHiHiHiHiHiHi"
'Tech > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Chapter 07: ๋ฐ๋ณต๋ฌธ (Loop) (0) | 2022.01.20 |
---|---|
Chapter 06: ๋์คํธ๋ญ์ฒ๋ง - ๊ตฌ์กฐ ๋ถํด ํ ๋น (Destructuring) (0) | 2022.01.19 |
Chapter 04: ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด (Template literals) (0) | 2022.01.16 |
Chapter 03: ํจ์ ๊ธฐ๋ณธ๊ฐ ์ธ์ (Default function arguments) (0) | 2022.01.13 |
์ฌ๋ค์ผ ๊ฐค๋ฌ๋ฆฌ ๊ตฌํ / Gallery with Thumbnail Slider / Swiper - Thumbs gallery (0) | 2022.01.11 |