본문 바로가기
CodeStates/└ JavaScript(Pre)

문자열 다루기

by Dream_World 2020. 6. 18.

str[index]

let str = 'SecurityInfo';

console.log(str[0]);     // S
console.log(str[2]);     // c
console.log(str[10]);    // f
console.log(str[15]);    // undefined
 
  • index로 접근은 가능하지만 쓸 수는 없음 (read-only)

toString

let str = 'Security';
let str1 = 'Info';
let str2 = '1';

console.log(str + str1);    // SecurityInfo
console.log(str2 + 1);      // 11
 
  • + 연산자를 쓸 수 있음

  • string 타입과 다른 타입 사이에 +연산자를 쓰면, string 형식으로 변환(toString)

concat

let str = 'Security';
let str1 = 'Info';
let str2 = '1';

str.concat(str1, str2);
"SecurityInfo1"
 
  • str.concat(str, str1 ...);

length PROPERTY

let str = 'SecurityInfo';

console.log(str.length);
12
 
  • 문자열의 전체 길이를 반환

str.indexOf(searchValue)

'Security Info'.indexOf('Secu');
0     // index 0
'Security Info'.indexOf('secu');
-1    // 찾는 문자열이 없으므로 -1
'Security Info'.indexOf('Info');
9     // index 9
'Security Info Info'.indexOf('Info');
9     // 처음으로 일치하는 index 9
'test'.lastIndexOf('e');
1     // 문자열 뒤에서부터 검색 : index 1
 
  • 처음으로 일치하는 index 검색

  • 찾는 문자열이 없으면 -1

  • lastIndexOf는 문자열 뒤에서부터 검색

str.includes

'Security Info'.includes('Secu');
true
'Security Info'.includes('secu');
false

str.split(seperator)

let str = 'Security Info';

console.log(str.split(' '));
["Security", "Info"]
  • 문자열 분리

  • 분리된 문자열 값은 배열

  • CSV(comma-separated values) 형식을 처리할 때 유용

  • CSV 파일 형식 : 연도,제조사,모델,설명,가격 1997,Ford,E350,"ac, abs, moon",3000.00 1999,Chevy,"Venture ""Extended Edition""","",4900.00 1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00 1996,Jeep,Grand Cherokee,"MUST SELL! air, moon roof, loaded",4799.00

  • 참조 사이트 : CSV - 위키백과

 

CSV (파일 형식) - 위키백과, 우리 모두의 백과사전

위키백과, 우리 모두의 백과사전. CSV(영어: comma-separated values)는 몇 가지 필드를 쉼표(,)로 구분한 텍스트 데이터 및 텍스트 파일이다. 확장자는 .csv이며 MIME 형식은 text/csv이다. comma-separated variables

ko.wikipedia.org

let csv = `연도,제조사,모델,설명,가격
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00`;

let lines = csv.split('\n') // 줄바꿈 split
lines[0]
"연도,제조사,모델,설명,가격"
lines[1]
"1997,Ford,E350,"ac, abs, moon",3000.00"
 

str.substring(start, end)

let str = 'Security Info';

console.log(str.substring(0, 3));    
// Sec   index 0,1,2        
console.log(str.substring(3, 0));    
// Sec   index 0,1,2  
console.log(str.substring(-1, 5));    
// Secur 음수는 0, index 0.1,2,3,4
console.log(str.substring(0, 20));    
// Security Info, index 범위 초과
 
  • 시작 index, 끝 index

str.toLowerCase() / str.toUpperCase()

console.log('SECURITYINFO'.toLowerCase());
securityinfo    // 소문자로 변환

console.log('securityinfo'.toUpperCase());
SECURITYINFO    // 대문자로 
 
  • 문자열 대, 소문자로 변환

'CodeStates > └ JavaScript(Pre)' 카테고리의 다른 글

push, pop, shift, unshift, slice  (0) 2020.06.18
타입 구분  (0) 2020.06.18
반복문  (0) 2020.06.17
배열  (0) 2020.06.17
'==' 와 ' ===' 비교  (0) 2020.06.16

댓글