ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1.Strings
    JavaScript 2021. 9. 27. 09:45
    반응형

    String은 ""나 ''형식으로 둘러싸서 작성한다.

    "This is a String" or 'This is a String'

     

    .toLowerCase() : string을 소문자로 변환한다.

    "BLUE".toLowerCase(); // "blue";

     

    .toUpperCase() : string을 대문자로 변환한다.

    "red".toUpperCase(); // "RED";

     

    길이구하기 : .length

    출력: console.log("출력할 내용");

    고정된 문장에서 특정값만 동적으로 출력하려면 백틱을 이용할 수 있다.

    function returnHTMLTag(label, value) {
        return `<tr><td>${label}</td><td>${value}</td></tr>`
    }
    
    console.log(returnHTMLTag("이름", "나이")); //<tr><td>이름</td><td>나이</td></tr>

     

    특정위치의 문자: string[idx]let sentence = "abcde";ex) console.log(sentence[0]); // sentence의 제일 처음 문자 출력 >> a

     

    문자열 합치기  "A" + "B" // "AB"

    .substring(시작위치, 종료위치) : 문자열 분할

    let example = "이 string을 쪼개 새로운 string을 얻는다.";
    console.log(example.substring(2, 8)); //string

    이런식으로도 응용이 가능하다.

    function capitalize(str) {
        return str[0].toUpperCase() + str.substring(1).toLowerCase();
    }
    
    console.log(capitalize("abcdefg")); //Abcdefg

    .trim() : 문자열에서 공백을 제거합니다.

    .startsWith("입력값"): 문자열의 첫번째 문자가 입력값과 같은지 비교합니다.

    .endsWith("입력값"): 문자열의 마지막 문자가 입력값과 같은지 비교합니다.

    .includes("입력값"): 문자열에 입력값이 존재하는지 비교합니다.

    .split("구분자"): 문자열을 구분자로 나누어 배열로 리턴합니다.

     

    let sentence = "   hello, world!   ";
    
    sentence = sentence.trim()//hello, world!>>문자열의 양 끝 공백을 제거합니다.
    console.log(sentence);
    
    console.log(sentence.startsWith("h")); //true
    console.log(sentence.startsWith()); //false >>파라미터를 입력하지 않거나 문자열에 입력값이 없다면 false를 리턴합니다.
    console.log(sentence.endsWith()); //false
    console.log(sentence.endsWith("!")); //true
    
    console.log(sentence.includes("hello")); //true
    console.log(sentence.includes("hahaha")); //false
    
    
    const splitedSentence = sentence.split(",");
    console.log(splitedSentence); //[ 'hello', ' world!' ]
    반응형

    'JavaScript' 카테고리의 다른 글

    6.Objects  (0) 2021.09.27
    5.Arrays  (0) 2021.09.27
    4.Conditions  (0) 2021.09.27
    3.variables  (0) 2021.09.27
    2.Numbers  (0) 2021.09.27

    댓글

Designed by Tistory.