Home
Map
String toLowerCase ExamplesTransform the cases of strings with the toLowerCase and toUpperCase functions.
Node.js
This page was last reviewed on Dec 13, 2023.
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.
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.
Info 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.
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
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.
var animal = "cat"; // Uppercase first letter in a string. var uppercase = animal[0].toUpperCase() + animal.substring(1); console.log("RESULT: " + uppercase);
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.
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
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.
Info We just append the original character. The result is built up as we go a long, and returned in the final line.
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
A review. For performance, we can avoid excessive string transformations. We can cache lowercase or uppercase forms, or store them directly in the program.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 13, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.