<JavaScript 총정리 4>

수민🐣·2022년 1월 28일
0

JavaScript

목록 보기
25/32

📚String.prototype.indexOf()

:특정문자가 문자열의 어느 index에 위치하는지 찾는 메소드이며 검색된 문자열이 '첫번째'로 나타나는 위치 index를 반환하고 찾는 문자열이 없으면 -1을 반환한다. 대소문자 구분 한다.
string.indexOf(searchvalue, position)
searchvalue : 필수 입력값, 찾을 문자열
position : optional, 기본값은 0, 문자열에서 찾기 시작하는 위치를 나타내는 인덱스, 값이 음의 정수이면 전체 문자열을 찾는다.

📚String.prototype.includes()

: 문자열 내에 찾고자 하는 다른 문자열이 있는지 확인 하는 메소드 이며 문자열을 찾아내면 true, 찾지 못하면 false로 반환한다. 대소문자 구분 한다.
string.includes(searchString[, position])
searchString : 이 문자열에서 찾을 다른 문자열
position Optional : searchString을 찾기 시작할 위치. 기본값 0.

📚String.prototype.slice()

: 문자열의 일부를 추출하면서 새로운 문자열을 반환한다.
string.slice(beginIndex[, endIndex])
beginIndex : 추출 시작점인 0부터 시작하는 인덱스
endIndex : 0부터 시작하는 추출 종료점 인덱스로 그 직전까지 추출

📚String.prototype.substring()

: string 객체의 시작 인덱스로 부터 종료 인덱스 전 까지 문자열의 부분 문자열을 반환한다.
string.substring(indexStart[, indexEnd])
indexStart : 반환문자열의 시작 인덱스
indexEnd : 옵션. 반환문자열의 마지막 인덱스 (포함하지 않음.)

  • indexEnd 가 생략된 경우, substring() 문자열의 끝까지 모든 문자를 추출
  • indexStart가 indexEnd와 같을 경우, substring() 빈 문자열을 반환
  • indexStart 가 indexEnd보다 큰 경우, 마치 두 개의 인자를 바꾼 듯 작동한다.

📚String.prototype.split()

: String 객체를 지정한 구분자를 이용하여 여러 개의 문자열로 나눈다. split는 문자열을 분할하는 메소드이다.
string.split( separator, limit )
separator : 분할의 기준
limit : 최대 분할 개수, 옵션. 값을 정하지 않으면 전체를 다 분할

 const str= '뜰에 뜰에 뜰에는 닭이 있다.';
    const str1 = 'The morning is upon us.';

    console.log(str.indexOf('뜰')); // 0
    console.log(str.indexOf('뜰',3)); // 3
    console.log(str.includes('뜰')); // true
    console.log(str.includes('가든')); // false

    console.log(str1.slice(5,8)); // orn
    console.log(str1.slice(1,8)); // he morn
    console.log(str1.slice(4,-2)); // morning is upon u
    console.log(str1.slice(12)); // is upon us.
    console.log(str1.slice(30)); // ""

    console.log(str1.substring(20)); // indexEnd 가 생략된 경우 모든 문자 추출 , 결과 : us.
    console.log(str.substring(5,5)); // 빈 문자열
    console.log(str1.substring(8,5)); // substring(5,8)처럼 작동 결과는 orn 
    console.log(str.substring(5, -2)); // -2는 0으로 간주하므로 substring(5,0) 이지만 
                                            //indexStart가 indexEnd가 크므로 substring(0,5)처럼 작동 결과는 뜰에 뜰에
    
    console.log(str.split('에',3)); // [ '뜰', ' 뜰', ' 뜰' ]
    console.log(str1.split('s')); // [ 'The morning i', ' upon u', '.' ]


📚String.prototype.match()

: 문자열이 정규식과 매치되는 부분을 검색한다. 일치하면, 일치하는 전체 문자열을 첫 번째 요소로 포함하는 Array를 반환한 다음 괄호 안에 캡처된 결과가 나온다. 일치하는 것이 없으면 null이 반환된다.
string.match(regexp)
regex : 정규식 개체, RegExp가 아닌 객체 obj가 전달되면, new RegExp(obj)를 사용하여 암묵적으로 RegExp로 변환되며 매개변수를 전달하지 않고 match()를 사용하면, 빈 문자열:[""]이 있는 Array가 반환된다.

const test  = 'love you. love me. love everything!'
    const regExp = /love/gi;    // gi는 대소문자 구분을 허용하지 않고 모든 패턴 검색
    test2 = test.match(regExp);
    console.log(test2); //[ 'love', 'love', 'love' ]


📚String.prototype.replace()

: 문자열에서 특정 문자열을 찾아서 첫번째로 찾은 문자열만 치환해주는 메소드이다. 대소문자 구분한다.
string.replace(regexp|substr, newSubstr|function)
regexp : 정규식(RegExp) 객체 또는 리터럴, newSubstr|function가 반환 한 값으로 대체 된다.
substr : newSubStr로 대체 될 String, 오직 첫 번째 일치되는 문자열만이 교체된다.
newSubStr : 첫번째 파라미터를 대신할 문자열(String)

const str3 = 'Twas the night before Xmas..., Xmas...';
    const newstr = str3.replace(/xmas/i, 'Christmas'); // i는 대상 문자열에 대해서 대/소문자를 식별하지 않는 것을 의미
    const newstr1 = str3.replace('Xmas', 'Christmas') // 오직 첫번째 문자열만 교체
    const newstr2 = str3.replace('xmas', 'Christmas') // 대소문자를 구분

    console.log(newstr);  // Twas the night before Christmas.., .Xmas...
    console.log(newstr1); // Twas the night before Christmas.., .Xmas...
    console.log(newstr2); // Twas the night before Xmas..., Xmas...    


📚String.prototype.toLowerCase()

: 문자열을 소문자로 변환해 새로운 문자열로 반환하는 메소드이다.
string.toLowerCase()

📚String.prototype.toUpperCase()

:문자열을 대문자로 변환해 새로운 문자열로 반환하는 메소드이다.
string.toUpperCase()

	console.log('SEUNGMINISCUTE'.toLowerCase()); // seungminiscute
        console.log('seungminiscute'.toUpperCase()); // SEUNGMINISCUTE 


📚String.prototype.concat()

: 매개변수로 전달된 모든 문자열을 호출 문자열에 뒤쪽에 연결해 붙인 결과인 새로운 문자열을 반환한다.

string.concat(string2, string3[, ..., stringN])
string2...stringN : 합칠 문자열

📚String.prototype.trim()

: 문자열 양 끝의 공백을 제거한다. 공백은 모든 개행문자와 space, tab, NBSP 등을 의미한다.
trim()

  • str 문자열에 공백이 없어도 예외가 발생하지 않고 새 문자열이 반환된다. (본질적으로 str의 복사본).
const str4 = ' Hello, ';
    console.log(str4.concat('seungmin ', '너 귀여운거 알어? ㅋㅅㅋ')); // Hello, seungmin 너 귀여운거 알어? ㅋㅅㅋ
    console.log(str4.trim()); // Hello,


📚String.length

: 문자열의 길이를 나타내는 메소드이다.

const str5 = 'String 객체 주요 메소드 끝!';
    console.log(str5.length); // 19

0개의 댓글

관련 채용 정보