728x90
모든 글자들의 나열을 문자열이라고 한다.
컴퓨터는 코드와 문자열을 구분하기 위해 작은따옴표(' ')나 큰따옴표(" ")를 사용하여 문자열을 구분한다.
문자열은 String이다. 문자 하나는 character이며, char이라는 축약어로 부르기도 한다. char가 포함된 메소드도 있다.
var str = 'Codestates';
console.log(str[0]); // 'C'
console.log(str[4]); // 'S'
console.log(str[10]); // undefined
// 변수[번호(0부터 시작)]로 그 번호에 해당하는 글자를 읽어낼 수 있다.
접근은 가능하지만 쓸 수는 없다.
str[0] = 'G';
console.log(str); // 'CodeStates' not 'GodeStates'
+연산자를 쓸 수 있다. string 타입과 다른 타입 사이에 + 연산자를 쓰면, string 형식으로 변환 (toString)
var str1 = 'Code';
var str2 = "States";
var str3 = '1';
console.log(str1 + str2); // 'CodeStates'
console.log(str3 + 7); // '17'
// str1.concat(str2, str3...); 의 형태로도 사용 가능
'1' + true // "1true"
'1' + 5 // "15"
'1' + [1,2,3] // "11,2,3"
.length는 문자열의 전체 길이를 반환한다.
var str = 'CodeStates';
console.log(str.length); // 10
.indexOf 는 찾고자 하는 문자열의 위치를 반환한다. 처음으로 일치하는 index를 반환, 문자열이 없으면 -1
.lastindexOf 는 문자열 뒤에서부터 찾는다.
'Blue Whale'.indexof('Blue'); // 0
'Blue Whale'.indexof('blue'); // -1
'Blue Whale'.indexof('Whale'); // 5
'Blue Whale Whale'.indexof('Whale); // 5
'canal'lastIndexOf('a') // 3
.includes 는 문자열 내에 단어가 존재하는지 Boolean 값을 리턴한다.
'blue whale'.includes('blue') // true
'blue whale'.includes('Blue') // false
.split 은 분리기준이 될 문자열을 입력하면 분리된 문자열이 포함된 배열을 리턴한다.
var str = 'Hello from the other side';
console.log(str.split(' '));
// ['Hello', 'from', 'the', 'other', 'side']
csv 형식을 처리할 때 유용하다.

.substring 은 시작 index 부터 끝 index 사이의 문자열을 반환한다.
var str = 'abcdefghij'
console.log(str.substring(0, 3)); // 'abc'
console.log(str.substring(3, 0)); // 'abc'
console.log(str.substring(1, 4)); // 'bcd'
console.log(str.substring(-1, 4); // 'abcd', 음수는 0으로 취급
console.log(str.substring(0, 20); // 'abcdefghij' index 범위를 초과하면 문자열 끝까지만.
// str.slice(start,end)와 비슷하나 substring은 원본을 해치지 않고 slice는 해친다는 차이점이 있다.
.toLowerCase() / toUpperCase() 는 각각 소문자로 변환한값, 대문자로 변환한 값을 리턴한다.
이 둘은 immutable, 즉 원본이 변하지 않는다.
console.log('ALPHABET'.toLowerCase()); // 'alphabet'
console.log('alphabet'.toUpperCase()); // 'ALPHABET'
let word = 'hello' // 'undefined'
word.toUpperCase() // "HELLO"
word // "hello"
// immutable, 원본이 변하지 않는다.
immutable 하게 바뀐 값을 되돌리려면 새로 대입하는 수밖에 없다.
var str = 'blue whale' // undefined
str.substring(0, 4) // "blue"
str // "blue whale"
str.toUpperCase() // "BLUE WHALE"
str // "blue whale"
str = str.substring(0, 4) // "blue"
str // "blue"
str = str.toUpperCase() // "BLUE"
str // "BLUE"
parseFloat() 함수는 문자열을 분석해 부동소수점 실수로 반환한다.

탬플릿 리터럴은 작은 따옴표나 이중따옴표 대신 백틱(``)을 사용한다.
표현식을 일반 문자열에 삽입하기 위해 아래의 문법을 사용해보고, 백틱을 이용해 더 읽기 쉽도록 만들어 보겠다.
var a = 5;
var b = 10;
console.log("Fifteen is " + (a + b) + " and\nnot " + (2 * a + b) + ".");
// "Fifteen is 15 and
// not 20."
백틱을 이용한 문법
var a = 5;
var b = 10;
console.log(`Fifteen is ${a + b} and
not ${2 * a + b}.`);
// "Fifteen is 15 and
// not 20."
반응형
'My Wiki (CodesStates) > JS,Node' 카테고리의 다른 글
(2-1) JS/Node 배열, 객체 - 배열 기초, 기초 메소드 (0) | 2021.07.31 |
---|---|
(1-6) JS/Node 기초 - 반복문 (0) | 2021.06.29 |
(1-4) JS/Node 기초 - 조건문 (0) | 2021.06.26 |
(1-3) JS/Node 기초 - 함수 (0) | 2021.06.26 |
(1-2) JS/Node 기초 - 타입 (0) | 2021.06.26 |
댓글