ToLowerCase
A letter can be in one of 2 states—uppercase or lowercase. In Node.js, we can transform cases dynamically with toLowerCase
and toUpperCase
.
In lowercasing strings, we transform only certain characters. Things like numbers and spaces and punctuation are left alone. Lowercasing can normalize input strings.
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.
string
we call these methods on is left unchanged and be used again.string
in mixed case (title case) and transform it to uppercase and then to all lowercase.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);BEFORE: Blue AFTER (UPPER): BLUE BEFORE: BLUE AFTER (LOWER): blue
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.
var animal = "cat"; // Uppercase first letter in a string. var uppercase = animal[0].toUpperCase() + animal.substring(1); console.log("RESULT: " + uppercase);RESULT: Cat
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.
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])); }STRING: cat UPPERFIRST: Cat STRING: UPPERFIRST: STRING: c UPPERFIRST: C STRING: dog UPPERFIRST: Dog STRING: BIRD UPPERFIRST: BIRD
With an iterative algorithm, we can uppercase all words in a string
. Here we loop over the characters in a 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"));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 program.