[JS] string methods

soor.dev·2021년 2월 15일
0

Java Script

목록 보기
1/26
post-thumbnail

split

: 스트링을 split(value) value 단위로 끊어서 각각의 스트링을 배열에 정리해줌

_The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method's call.

const str = 'Hi, my name is Hyunsoo';
const split = str.split('');
console.log(split)
//output : [ 'Hi,', 'my', 'name', 'is', 'Hyunsoo' ]

localCompare

: a배열 안에 있는 값들을 비교하여, 유니코드 순으로 배열해줌

MDN_localeCompare


substr

: substr 메서드는 특정 위치에서 시작하여 특정 문자 수 만큼을 반환함

The substr()method returns a portion of the string, starting at the specified index and extending for a given number of characters afterwards.

# 1. 아래와 같이 s라는 문자열의 3번 인덱스(d)부터 2개의 문자를 가져와서 de가 반환됨

s = 'abcde'
const x = s.substr(3, 2);
console.log(x); // output : de

# 2. s라는 문자열의 길이를 변수로 지정하여 인덱스를 지정했는데 이 때, x/2가 정수로 떨어지지 않더라도 Math.floor()적용시킨 것 처럼 정수로 떨어져서 반환됨

s = 'abcde'
const x = s.length;  // 5
const y = s.substr(x/2, 2); // 인덱스 2부터 2개의 문자를 반환 : cd

0개의 댓글