- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert string to lowercase in TypeScript?
In this tutorial, we will learn to convert a string into lowercase in TypeScript. Converting string to lowercase is a basic operation, but it is useful many times.
We can think about implementing the search functionality in the app. When users search for any phrase or word in lowercase, uppercase, or mixed case, we need to convert the string into lowercase and then execute the search query in the database to find the related items.
Using the toLowerCase() Method
The toLowerCase() method allows us to convert all alphabetic characters of the string into lowercase. If a character is in uppercase, it converts it to lowercase. Otherwise, it remains as it is. Also, it doesn’t change the special characters or numbers.
Users can follow the syntax below to convert the string to lowercase using the toLowerCase() method in TypeScript.
Syntax
let str: string = "TutorialsPoint"; str = str.toLowerCase();
In the above syntax, we have invoked the toLowerCase() method on the string, which means the object of the string class.
Example 1
In the example below, we convert a string to lowercase. The string contains only alphabets with some uppercase letters.
let str: string = "Welcome to TutorialsPoint"; console.log("Before: " + str); str = str.toLowerCase(); console.log("After: " + str);
On compiling, it will generate the following JavaScript code −
var str = "Welcome to TutorialsPoint"; console.log("Before: " + str); str = str.toLowerCase(); console.log("After: " + str);
The above code will produce the following output −
Output
Before: Welcome to TutorialsPoint After: welcome to tutorialspoint
Example 2
In the example below, we convert a string to lowercase. The string contains alphanumeric chars and some special chars.
// String with alphanumeric chars let str: string = "Tut123Point"; str = str.toLowerCase(); console.log(str); // String with special chars str = "Tut1:,.Point@3"; str = str.toLowerCase(); console.log(str);
On compiling, it will generate the following JavaScript code −
// String with alphanumeric chars var str = "Tut123Point"; str = str.toLowerCase(); console.log(str); // String with special chars str = "Tut1:,.Point@3"; str = str.toLowerCase(); console.log(str);
The above code will produce the following output −
Output
tut123point tut1:,.point@3
Custom Algorithm to Convert String to Lowercase
The other way to convert a string to lowercase in TypeScript is that iterate through every character of the string and convert every character in lowercase.
Steps
STEP 1 − Define two string-type variables. Assign the original string value to the first variable. We will assign the converted string to the second variable.
STEP 2 − Next, we use the for loop to iterate through the string characters. For every character, we find the ASCII value using the charCodeAt() method of the string. If we find an ASCII value between 65 and 90 (including), the alphabetic character is in uppercase, and we need to convert it into lowercase.
STEP 3 − To convert uppercase characters to lowercase, add 32 to the ASCII value of uppercase later, and use the fromCharCode() method of the string class to get a character from its ASCII value.
STEP 4 − Append the converted character to the second string and, at last, printing the lower string.
Example
In the example below, we convert a string containing some uppercase letters to lowercase.
let stringName: string = "Hi @312 User! Are YOU THERE?"; let lower: string = ""; for (let i = 0; i < stringName.length; i++) { let value = stringName.charCodeAt(i); if (value >= 65 && value <= 90) { lower += String.fromCharCode(value + 32); } else { lower += stringName[i]; } } console.log(lower);
On compiling, it will generate the following JavaScript code −
var stringName = "Hi @312 User! Are YOU THERE?"; var lower = ""; for (var i = 0; i < stringName.length; i++) { var value = stringName.charCodeAt(i); if (value >= 65 && value <= 90) { lower += String.fromCharCode(value + 32); } else { lower += stringName[i]; } } console.log(lower);
The above code will produce the following output &miinus;
hi @312 user! are you there?
Users learned two ways to convert the string into lowercase. The first way is the simplest, as we need to write a single line of code. The second method demonstrates how the toLowerCase() method is implemented manually in the string class. So, it is recommended to use the first method. The second method is only to demonstrate how the toLowerCase() method works internally.
no