새소식

반응형
WEB/JavaScript

[JavaScript] JS String(문자열) 대 / 소 문자 변환 방법

  • -
반응형

2021-04-05


 

간혹 문자열을 대소문자로 치환해야 하는 경우가 있다. 이러한 경우 손쉽게 해당 문제를 해결하는 방법을 알아보자.


- 대문자 변환 [ toUpperCase() ]

 

기본적인 문자열을 전부 대문자로 바꿔야 하는 경우가 있는데 이경우 toUpperCase() 메서드를 사용하면 손쉽게 변경할 수 있다. 아래의 예제를 보자

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript"> 
	
	var text = "Hello World Hello everyone!"
	console.log(text.toUpperCase()) // HELLO WORLD HELLO EVERYONE!

</script>
</head>
<body>
Hello World

</body>
</html>​

 

 

 

브라우저 콘솔창의 출력을 확인해보면, HELLO WORLD HELLO EVERYONE! 로 나타난다. 


- 소문자 변환 [ toLowerCase() ]

 

반면에 기존의 문자열을 전부 소문자로 바꿔야 하는 경우에는 toLowerCase() 메소드를 사용하면 손쉽게 변경할 수 있다. 다음의 예제를 살펴보자.

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript"> 
	
	var text = "Hello World Hello everyone!"
	console.log(text.toLowerCase()) // hello world hello everyone!

</script>
</head>
<body>
Hello World

</body>
</html>​

 

브라우저 콘솔창의 출력을 확인해보면, hello world hello everyone! 로 나타난다. 

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.