<script> var strLower = "change to all uppercase"; var strUpper = strLower.toUpperCase(); alert(strUpper); </script>
<script> var strUpper = "CHANGE TO ALL LOWERCASE"; var strLower = strUpper.toLowerCase(); alert(strLower); </script>
A common task is to change the first letter in a string to uppercase.
The above example uses str.charAt(0) to access the first letter, then uses str.substring(start, stop) to access the rest of the string, then concatenates the two parts.
You can capitalize any character in a string by using the str.indexOf(char) method to get the index of the letter.
The above example uses indexOf(char) to access the index of the first instance of the desired letter. Then uses str.toUpperCase() to change it to upper case. The uses str.substring(start, stop) to access the part of the string before the letter, and the part of the string after the letter. Then concatenates the three parts.
If you want to capitalize a character that is not the first instance of the desired letter, you can use str.indexOf(searchElement, fromIndex). The second argument is an index from which to start looking fo the letter. In the example above, it finds the second occurrence of "c", in the word "can" and changes that to upper case.
Learn more at amazon.com