let string = "몇 글자일까요 ?" console.log(string.length);
결과 👉🏻 9
let string = "몇 글자일까요 ?" for (let char of string) { console.log(char); }
결과 👇🏻
'몇' ' ' '글' '자' '일' '까' '요' ' ' '?'
LowerCase (), toUpperCase ()
let string = "heLLo ?" console.log(string.toUpperCase());
결과 👉🏻 'HELLO ?'
let string = "heLLo ?" console.log(string.toLowerCase());
결과 👉🏻 'hello ?'
문자열 내에서 하위 문자열을 찾는 메소드이다.
지정된 위치에서 시작하여 찾고 싶은 문자열의 위치를 반환한다.
찾을 수 없을 경우 -1을 반환한다.
str.lastIndexOf(substr, position)
let string = "heLLo hi?" console.log(string.indexOf("h")); ///"h"라는 문자를 0번째부터 찾는다. console.log(string.indexOf("h",1));///"h"라는 문자를 문자열의 index 1부터 찾는다. console.log(string.indexOf("h",7));///"h"라는 문자를 7번째부터 찾는다.
결과 👇🏻
0
6
-1
"h"를 문자열의 0부터 찾아보면 가장 먼저 나오는 "h"는 index 0의 자리에 있다
"h"를 문자열의 1부터 찾아보면 가장 먼저 나오는 "h"는 index 6의 자리에 있다.
7이후부터는 "h"가 존재하지 않아 -1을 반환한다.
str.slice(start, end)
let string = "heLLo hi?" console.log(string.slice(2,4));
결과 👉🏻 'LL'
str.substr(start, length)
let string = "heLLo hi?" console.log(string.substr(1,4));
결과 👉🏻 'eLLo'
let string = "heLLo hi ? " console.log(string.repeat(4)); console.log("hey ".repeat(3));
결과 👇🏻
'heLLo hi ? heLLo hi ? heLLo hi ? heLLo hi ? '
'hey hey hey '