string

Seunghyunkim1·2020년 4월 3일
0

wecode

목록 보기
5/25

https://javascript.info/string
https://github.com/airbnb/javascript //보기좋은 코딩콘벤션
https://www.w3schools.com/jsref/jsref_obj_string.asp //string함수들

toUpperCase 와 toLowerCase함수호출

let lastName = 'Yeri Kim';
let upperLastName = lastName.toUpperCase();
let lowerLastName = lastName.toLowerCase();
console.log(lastName);
console.log(upperLastName);
console.log(lowerLastName);
-->Yeri Kim
YERI KIM
yeri kim

대소문자구분해야합니다.

console.log('python' === 'python');
console.log('Python' === 'python');

문자길이

  • length 속성으로 string형의 length 확인 가능
if (phoneNumber.length !== 10 &&  phoneNumber.length !== 11) {
  alert("폰번호 제대로 입력하셨나요?");
}

핸드폰번호 ex)01012341234

숫자로 표현하면 시작점인 0을 인식못함
string으로 표현해야 "0"도 갖고 있게 된다.

두번째와같이 표현해야 길이 확인가능

  • var phoneNumber = 01012349876;
    console.log(phoneNumber);
    console.log(phoneNumber.length);
  • let phoneNumber = "01012349876";
    console.log(phoneNumber);
    console.log(phoneNumber.length);

문자열찾기 & 문자수정

  • info 문구에서 "프로래밍"이 시작되는 index는 12입니다.
  • if (firstChar !== -1)
  • firstChar이 -1이 아니면
    = "프로래밍"이라는 문구가 없지 않다면
    = "프로래밍"이라는 문구가 있다면
    = "프로래밍" 문구는 12번째에서 시작하므로 firstChar는 12 이라는 뜻입니다.
    "프로래밍"이라는 오타가 포함되어있으면 if문을 실행합니다.
let info = "JavaScript는 프로래밍 언어이다.";
let firstChar = info.indexOf("프로래밍"); 
console.log(info, firstChar);


if (firstChar !== -1) { 
  info = info.slice(0, firstChar) + "프로그래밍" + info.slice(firstChar+4, info.length); 
}
console.log(info);

slice(시작점, 끝점) 인자두개
slice(시작점부터끝) 인자한개

0개의 댓글