TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVASCRIPT

JavaScript toLowerCase Examples

Transform the cases of strings with the toLowerCase and toUpperCase functions.
ToLowerCase. A cat sleeps. A cat is sometimes awake. It is the same cat in either state, but with a different level of awareness.
Like the cat, a letter can be in one of two states—uppercase or lowercase. Characters have cases. In JavaScript we can transform cases dynamically with toLowerCase and toUpperCase.
First example. Here is a simple example. We use the methods toUpperCase and toLowerCase—these are the same as ToUpper, ToLower, upper, lower, upcase and downcase methods in other languages.

Example: In case you are wondering, here is how you can use toUpperCase and toLowerCase.

Original: The original string we call these methods on is left unchanged and be used again.

Here: We take a string in mixed case (title case) and transform it to uppercase and then to all lowercase.

Strings
JavaScript program that uses toUpperCase, toLowerCase var color = "Blue"; // Create a copy of the string and uppercase it. // ... The original is left alone. var upper = color.toUpperCase(); console.log("BEFORE: " + color); console.log("AFTER (UPPER): " + upper); // Lowercase a string. var lower = upper.toLowerCase(); console.log("BEFORE: " + upper); console.log("AFTER (LOWER): " + lower); Output BEFORE: Blue AFTER (UPPER): BLUE BEFORE: BLUE AFTER (LOWER): blue
Uppercase first. Here we have the string "cat" and we transform the string. We access the first letter with the index 0, and uppercase this entire string. Then we add the remaining part.

Tip: This method works well on strings of 1 character or more. And it can be placed in a function for easier reuse.

JavaScript program that uppercases first letter var animal = "cat"; // Uppercase first letter in a string. var uppercase = animal[0].toUpperCase() + animal.substring(1); console.log("RESULT: " + uppercase); Output RESULT: Cat
Uppercase first, function. Here we introduce the upperFirst function. It tests the argument to see if it has a length of 1 or greater. And then it uppercases the first letter in the string.

Tip: This function handles zero-length strings. For strings with an initial letter already uppercased, it changes nothing.

JavaScript program that uses function function upperFirst(value) { if (value.length >= 1) { return value[0].toUpperCase() + value.substring(1); } return value; } // Test upperFirst on many strings. var values = ["cat", "", "c", "dog", "BIRD"]; for (var i = 0; i < values.length; i++) { console.log("STRING: " + values[i]); console.log("UPPERFIRST: " + upperFirst(values[i])); } Output STRING: cat UPPERFIRST: Cat STRING: UPPERFIRST: STRING: c UPPERFIRST: C STRING: dog UPPERFIRST: Dog STRING: BIRD UPPERFIRST: BIRD
Uppercase all words. With an iterative algorithm, we can uppercase all words in a string. Here we loop over the characters in a string.

And: On the first character, or a lowercase-range ASCII letter following a space, we append the uppercase form of the letter.

For other chars: We just append the original character. The result is built up as we go a long, and returned in the final line.

JavaScript program that uppercases all words in string function uppercaseAllWords(value) { // Build up result. var result = ""; // Loop over string indexes. for (var i = 0; i < value.length; i++) { // Get char code at this index. var code = value.charCodeAt(i); // For first character, or a lowercase range char following a space. // ... The value 97 means lowercase A, 122 means lowercase Z. if (i === 0 || value[i - 1] === " " && code >= 97 && code <= 122) { // Convert from lowercase to uppercase by subtracting 32. // ... This uses ASCII values. result += String.fromCharCode(code - 32); } else { result += value[i]; } } return result; } console.log("RESULT: " + uppercaseAllWords("welcome visitors, enjoy your stay")); Output RESULT: Welcome Visitors, Enjoy Your Stay
For performance, we can avoid excessive string transformations. We can cache lowercase or uppercase forms, or store them directly in the JS source.
In lowercasing strings, we transform only certain characters. Things like numbers and spaces and punctuation are left alone. Often it is best to cache the required string in your JS source.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf