JavaScript / Object / String.toUpperCase(), String.toLowerCase()
목차
.toUpperCase()
문법
.toUpperCase()는 대문자로 변환하는 메서드이다. 예를 들어 다음은 ABC를 반환한다.
'abc'.toUpperCase()
예제 1 - 대문자로 변환하여 출력
- 문자열 Aaa를 대문자로 변환하여 출력한다.
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> * { font-family: Consolas, monospace; font-size: 20px; } </style> </head> <body> <script> document.write( 'Aaa'.toUpperCase() ); </script> </body> </html>
예제 2 - 폼에 입력 시 대문자로 변환
- 폼에 문자를 입력할 때 실시간으로 대문자로 변환한다.
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <style> * { font-family: Consolas, monospace; font-size: 20px; } </style> <script src="//code.jquery.com/jquery-3.3.1.min.js"></script> </head> <body> <p> <input type="text" name="tuc" id="tuc"> </p> <script> $( '#tuc' ).on( 'keyup', function() { var tuc = $( '#tuc' ).val(); $( '#tuc' ).val( tuc.toUpperCase() ); } ); </script> </body> </html>
.toLowerCase()
문법
.toLowerCase()는 소문자로 변환하는 메서드이다. 예를 들어 다음은 abc를 반환한다.
'ABC'.toLowerCase()
예제 1 - 소문자로 변환하여 출력
- 문자열 Aaa를 소문자로 변환하여 출력한다.
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> * { font-family: Consolas, monospace; font-size: 20px; } </style> </head> <body> <script> document.write( 'Aaa'.toLowerCase() ); </script> </body> </html>
예제 2 - 폼에 입력 시 소문자로 변환
- 폼에 문자를 입력할 때 실시간으로 문자로 변환한다.
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <style> * { font-family: Consolas, monospace; font-size: 20px; } </style> <script src="//code.jquery.com/jquery-3.3.1.min.js"></script> </head> <body> <p> <input type="text" name="tlc" id="tlc"> </p> <script> $( '#tlc' ).on( 'keyup', function() { var tlc = $( '#tlc' ).val(); $( '#tlc' ).val( tlc.toLowerCase() ); } ); </script> </body> </html>