2012.01.19(화) TIL(2) - 문자열(String)

HJ's Coding Journey·2021년 1월 19일

CodeStates Pre-Course

목록 보기
4/23

<문자열 (String)>

  • str[index]
let str = 'CodeStates';
console.log(str[0]); // 'C'
  • concatenating strings
let str1 = 'Code';
let str2 = 'States';
let str3 = '1';
console.log(str1 + str2); // 'CodeStates'
console.log(str3 + 7); // '17'

str1.concat(str2, str3); // 'CodeStates1'
  • str.length
let str = 'CodeStates';
console.log(str.length); // 10
  • str.indexOf
'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
  • str.includes -> true / false
'Blue Whale'.includes('Blue'); // true
'canal'.includes('b'); // false
  • str.split
let str = 'Hello from the other side';
console.log(str.split(''); // ['Hello', 'from', 'the', 'other', 'side']
  • str.substring (str.slice)
let 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'
console.log(str.substring(0, 20); // 'abcdefghij'
  • str.toLowerCase() / str.toUpperCase()
console.log('ALPHABET'.toLowerCase()) // 'alphabet'
console.log('alphabet'.toUpperCase()) // 'ALPHABET'
profile
Improving Everyday

0개의 댓글