- 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 uppercase in TypeScript?
In this TypeScript tutorial, we will learn to convert the string to uppercase. We need to convert every single alphabetic character to uppercase to convert the whole string uppercase by keeping the numbers and special characters as it is.
As a beginner TypeScript programmer, maybe a question can arise in your mind what is the need to convert the string to uppercase? Here is the very straightforward answer. Have you ever used the search bar of any website, which shows different results when you search? If yes, search the uppercase and normal strings, which will return the search result. It means that whatever you will search in the search bar, the server either converts it to uppercase or lowercase and then matches it with the database.
Also, while filling out the forms on some websites, you have observed that even if you enter the characters in lowercase, it converts to uppercase. So, there can be many uses for converting strings to uppercase.
Here, we will learn two methods to convert the string to uppercase in TypeScript.
Use the String.toUpperCase() Method of TypeScript
The toUpperCase() method is the built-in library method in TypeScript which converts the string to uppercase. We can invoke the toUpperCase() method by taking a string as a reference. Also, TypeScript doesn’t allow invoking the toUpperCase() method on any other data type variable, unlike JavaScript, as it is type-safe.
Syntax
Users can follow the syntax below to convert the string to uppercase using the toUpperCase() method in the TypeScript.
let emp_name: string = "Shubham Vora"; let result = emp_name.toUpperCase()
Steps
Step 1 − Define a string with some lowercase letters.
Step 2 − Convert the above-defined string to uppercase using the toUpperCase() method.
Step 3 − Print the converted string.
Example 1
In the example below, we have declared a string. After that, we used the toUpperCase() method to convert the string to uppercase.
// Creating a string let str: string = "Tutorials Point, Simply Easy Learning"; console.log("Before converting to Uppercase: " + str ) // Converting the string to uppercase let str_upper = str.toUpperCase(); console.log( "After converting to uppercase: " + str_upper);
On compiling, it will generate the following JavaScript code −
// Creating a string var str = "Tutorials Point, Simply Easy Learning"; console.log("Before converting to Uppercase: " + str); // Converting the string to uppercase var str_upper = str.toUpperCase(); console.log("After converting to uppercase: " + str_upper);
Output
The above code will produce the following output −
Before converting to Uppercase: Tutorials Point, Simply Easy Learning After converting to uppercase: TUTORIALS POINT, SIMPLY EASY LEARNING
Example 2
In the example below, we have declared a string containing some special characters and numbers also. After that, we used the toUpperCase() method to convert the string to uppercase.
// Creating a string let str: string = "@weds!23sd$ #$3c ^%^Gf"; console.log(" Before converting to Uppercase: " + str); // Converting the string to uppercase let upper_str = str.toUpperCase(); console.log("After converting to Uppercase: " + upper_str);
On compiling, it will generate the following JavaScript code −
// Creating a string var str = "@weds!23sd$ #$3c ^%^Gf"; console.log(" Before converting to Uppercase: " + str); // Converting the string to uppercase var upper_str = str.toUpperCase(); console.log("After converting to Uppercase: " + upper_str);
Output
The above code will produce the following output −
Before converting to Uppercase: @weds!23sd$ #$3c ^%^Gf After converting to Uppercase: @WEDS!23SD$ #$3C ^%^GF
In the example output, users can observe that the toUpperCase() method leaves special characters and numbers as it is and converts the alphabetical characters to uppercase.
Use The toLocaleUpperCase() Method of TypeScript
The toLocaleUpperCase() method is also a built-in library method in TypeScript and returns the same result as the toUpperCase() in most cases. It returns the uppercase characters of the string after case mappings. Some locales, such as Turkish, don’t follow the case mappings; in such cases, the toLocaleUpperCase() method returns a different result.
Syntax
In the syntax below, we have demonstrated how to use the toLocaleUpperCase() method of TypeScript to convert the string to uppercase.
let message: string = "Hello World!"; let result = emp_name.toLocaleUpperCase( loclaes );
Parameters
The toLocaleUpperCase() method takes a single parameter.
locales − It is an optional parameter. It is a language tag that represents according to which specific regional language needs to convert the string to uppercase.
Example 1
In the example below, we define a string. We convert the string to uppercase according to “lt-LT” locale-specific.
let str: string = "Tutorials Point"; console.log("Before converting to Uppercase: ", str) // Using the toLocaleUpperCase() method let upp_str = str.toLocaleUpperCase("lt-LT"); console.log("After converting to Uppercase: " + upp_str);
On compiling, it will generate the following JavaScript code −
var str = "Tutorials Point"; console.log("Before converting to Uppercase: ", str); // Using the toLocaleUpperCase() method var upp_str = str.toLocaleUpperCase("lt-LT"); console.log("After converting to Uppercase: " + upp_str);
Output
The above code will produce the following output −
Before converting to Uppercase: Tutorials Point After converting to Uppercase: TUTORIALS POINT
Example 2
In the example below, we define a string. We use toLocaleUpperCase() method to convert the string to uppercase. We pass "en-US" as a locale parameter to this method.
let str: string = "Tutorials Point"; console.log("String before converting to Uppercase: ", str) // Using the toLocaleUpperCase() method let upp_str = str.toLocaleUpperCase("en-US"); console.log("String after converting to Uppercase: " + upp_str);
On compiling, it will generate the following JavaScript code −
var str = "Tutorials Point"; console.log("String before converting to Uppercase: ", str); // Using the toLocaleUpperCase() method var upp_str = str.toLocaleUpperCase("en-US"); console.log("String after converting to Uppercase: " + upp_str);
Output
The above code will produce the following output −
String before converting to Uppercase: Tutorials Point String after converting to Uppercase: TUTORIALS POINT
Users learned two different methods to convert the string to uppercase in TypeScript. However, both method returns the same result except for some Unicode characters whose default mapping is not. It is recommended to use the toUpperCase() method to convert the string to uppercase in TypeScript.
- Related Articles
- How to convert string to number in TypeScript?
- How to convert string to lowercase in TypeScript?
- How to Convert Date to String in TypeScript?
- How to convert a string into uppercase in AngularJS?
- Convert a String to Uppercase in C
- How to convert lowercase letters in string to uppercase in Python?
- Convert string to lowercase or uppercase in Arduino
- How to convert all uppercase letters in string to lowercase in Python?
- Swift Program to Convert a String to Uppercase
- Golang Program to convert a string into Uppercase
- Python program to convert a string into uppercase
- For the Hosts Locale how to convert a string to uppercase letters in JavaScript?
- Java program to convert a string to lowercase and uppercase.
- Write a C program to convert uppercase to lowercase letters without using string convert function
- How to perform string comparison in TypeScript?
