개발/번역

String methods : startsWith in Javascript.

beforesol 2019. 8. 14. 14:41

Javascript에서 string 객체의 startsWith 메소드에 대해 배워봅시다.

 

startsWith() 메서드는 주어진 문자가 문자열의 시작 부분에 있으면 true를 반환하고, 그렇지 않으면 false를 반환합니다.

const  str = "Javascript Jeep is";
str.startsWith("Java"); // true

// case sensitive
str.startWith("JAVA"); // false

이 방법을 대소 문자를 구분하지 않으려면 사용자 정의 함수를 작성할 수 있습니다.

String.prototype.startsWithIgnoreCase = function(str) {
	// this points to the string
    
    return this.toLowerCase().startsWith(str.toLowerCase());
}

str.startWithIgnoreCase("JAVA"); // true

 

또한 검색을 시작할 위치를 지정할 수도 있습니다.

const str = "Javascript Jeep is";
str.startsWith("Jeep", 11) // true

 

 

원문: https://medium.com/front-end-weekly/string-methods-startswith-in-javascript-5971a849ed5

 

String methods : startsWith in Javascript.

Learn about the startsWith method of string object in Javascript.

medium.com