TIL 41 | String Method, Property

Saemsol Yoo·2021년 1월 17일
0

javascript

목록 보기
25/25
post-thumbnail

1. String properties

string.length

→ 문자열의 길이 를 나타낸다.

const string = "Hello World";
string.length //11



2. String Methods

string.concat( )

→ 매개변수로 전달된 모든 문자열을 호출한 문자열에 붙여서 새로운 문자열을 반환한다.

  • 반환값 : 주어진 문자열을 모두 붙인 새로운 문자열
  • ✨ 원본문자열에는 영향을 미치지 않는다.
const str1 = 'Hello';
const str2 = 'World';

console.log(str1.concat(' ', str2));   //'Hello World'
console.log(str2.concat(' ~ ', str1));   //'World ~ Hello'

string.endWith()

특정 문자열로 끝나는지를 확인시켜준다.

  • 반환값 : true / false
  • ✨ 원본문자열에는 영향을 미치지 않는다.
const string = "it's gonna be alright!";

console.log(string.endsWith('gonna'));  //false
console.log(string.endsWith('be'));  //false
console.log(string.endsWith('alright'));  //false
console.log(string.endsWith('alright!'));  //true

string.includes()

→ 특정 문자열이 다른 문자열에 포함되어있는지 를 확인시켜준다.

  • 반환값 : true / false
  • ✨ 원본문자열에는 영향을 미치지 않는다.
const sentence = "it's gonna be alright!";
const word = 'star';
const word2 = 'alright';

console.log(sentence.includes(word));  //false
console.log(sentence.includes(word2));  //true
console.log(sentence.includes("be"));  //true

string.indexOf()

→ 호출한 string 객체에서 주어진 값과 일치하는 첫 번째 인덱스를 반환해준다.

  • 반환값
    - 일치하는 값이 있으면 → 첫번째로 일치하는 index
    - 일치하는 값이 없으면 → -1 반환
    - 만약 동일한 문자가 있다면 앞쪽에 먼저 있는 index가 반환된다!
  • string.indexOf() 에 찾으려는 문자열 뿐만 아니라, 특정 index 값을 전달하면 그 index부터 찾는다!
  • 대소문자를 구분한다!
  • ✨ 원본문자열에는 영향을 미치지 않는다.
const sentence = "it's gonna be alright!";
const word = 'star';
const word2 = 'alright';

console.log(sentence.includes(word));  //false
console.log(sentence.includes(word2));  //true
console.log(sentence.includes("be"));  //true
console.log(sentence.indexOf("Be"));  //-1

string.lastIndexOf()

→ 호출한 string 객체에서 주어진 값과 일치하는 부분을 뒤에서부터 역순으로 탐색하여 최초로 마주치는 인덱스를 반환해준다.

  • 반환값
    - 일치하는 값이 있으면 → 역순으로 거슬러처 최초로 일치하는 index
    - 일치하는 값이 없으면 → -1 반환
    - 만약 동일한 문자가 있다면 뒤쪽에 있는 index가 반환된다!
  • 대소문자를 구분한다!
  • ✨ 원본문자열에는 영향을 미치지 않는다.
const sentence = "it's gonna be alright! alright!";

console.log(sentence.lastIndexOf("alright"));  //23
console.log(sentence.lastIndexOf("alright", 20));  //14
console.log(sentence.indexOf("alright"));  //14

string.repeat()

→ 문자열을 주어진 횟수만큼 반복해서 붙인 새로운 문자열을 반환한다.

  • 반환값 : 지정해준 횟수만큼 반복되어 붙여진 새로운 문자열
  • ✨ 원본문자열에는 영향을 미치지 않는다.
"Hello".repeat(4);     //'HelloHelloHelloHello'

const origin = "Welcome";
const repeat = origin.repeat(4);

console.log(origin);  //'Welcome'
console.log(repeat);  //'WelcomeWelcomeWelcomeWelcome'

string.replace()

→ 해당하는 부분의 문자열을 주어진 문자열로 변환시켜서 새로운 문자열을 반환한다.

  • 반환값 : 주어진 문자열로 변환되어 바뀐 새로운 문자열
  • 단, 해당하는 모든 문자열이 교체되는건 아니고, indexOf 처럼 처럼 만나는 부분에 대해서 교체가 된다.
  • 모두 바꾸고 싶으면 stirng.repeatAll( ) 을 쓰면 된다!
  • ✨ 원본문자열에는 영향을 미치지 않는다.
const string = "I will always love you!";
const newString = string.replace("love", "like");

console.log(string);
console.log(newString);

string.slice()

전달한 인덱스부터 endIndex 직전값까지 반환한다.

  • 반환값 : 주어진 문자열로 변환되어 바뀐 새로운 문자열
  • endIndex를 뒤에서부터 카운트 하고 싶으면 음수값 을 전달해주면 된다!
  • ✨ 원본문자열에는 영향을 미치지 않는다.
const string = "I will always love love you! ";

//endIndex 입력 안해주면 마지막까지 추출해준다.
const newString = string.slice(7);
//endIndex를 입력해주면, 입력해준 인덱스는 포함하지 않고 출력해준다..
const newString2 = string.slice(14,18);

console.log(string);      //'I will always love love you! '
console.log(newString);   //'always love love you! '
console.log(newString2);  //'love'

string.split()

→ String 객체를 지정한 구분자를 이용해서 여러 개의 문자열로 나눠 배열에 담아준다.

  • 반환값 : 지정해준 구분자로 나눠진 문자열이 담긴 배열
  • ✨ 원본문자열에는 영향을 미치지 않는다.
const string = "one,two,three,four,five";

const newArray = string.split(',');

console.log(string);      //'one,two,three,four,five'
console.log(newArray);   //[ 'one', 'two', 'three', 'four', 'five' ]
profile
Becoming a front-end developer 🌱

0개의 댓글

관련 채용 정보