자바스크립트에서 문자열의 첫글자를 대문자로 만드는법
조회수 7999회
this is a test -> This is a test the Eiffel Tower -> The Eiffel Tower /index.html -> /index.html
이런식으로 자바스크립트에서 문자열의 첫글자를 대문자로 만들고싶은데 방법을 모르겠습니다. 어떻게 하면 좋을까요?
1 답변
-
이런식으로 해봤습니다.
function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); }
String.prototype.capitalizeFirstLetter = function() { return this.charAt(0).toUpperCase() + this.slice(1); }
또 이런식으로 작성하면
'string'.capitalizeFirstLetter() // String
이렇게 쓸수도 있습니다.
댓글 입력