In this chapter, we will explore the toUpperCase()
method in TypeScript. This method is a built-in function that helps in converting all the characters in a string to uppercase. Understanding how to use toUpperCase()
is useful for standardizing string cases and performing case-insensitive comparisons.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The toUpperCase()
method returns the calling string value converted to uppercase. This method does not affect the original string but returns a new string with all uppercase characters.
2. Syntax
string.toUpperCase();
Parameters
The toUpperCase()
method does not take any parameters.
Return Value
The method returns a new string representing the calling string converted to uppercase.
3. Examples
Let's look at some examples to understand how toUpperCase()
works in TypeScript.
Example 1: Basic Usage
In this example, we convert a string to uppercase.
let str: string = "Hello, TypeScript!";
let result = str.toUpperCase();
console.log(result); // Output: HELLO, TYPESCRIPT!
Example 2: Mixed Case String
In this example, we convert a string with mixed case characters to uppercase.
let str: string = "TypeScript Is Awesome!";
let result = str.toUpperCase();
console.log(result); // Output: TYPESCRIPT IS AWESOME!
Example 3: String with Numbers and Symbols
In this example, we convert a string containing numbers and symbols to uppercase. Note that numbers and symbols are unaffected by the toUpperCase()
method.
let str: string = "123 abc! @#";
let result = str.toUpperCase();
console.log(result); // Output: 123 ABC! @#
Example 4: Using toUpperCase()
for Case-Insensitive Comparison
In this example, we use toUpperCase()
to perform a case-insensitive comparison between two strings.
let str1: string = "TypeScript";
let str2: string = "typescript";
if (str1.toUpperCase() === str2.toUpperCase()) {
console.log("The strings are equal (case-insensitive).");
} else {
console.log("The strings are not equal.");
}
// Output: The strings are equal (case-insensitive).
4. Conclusion
In this chapter, we explored the toUpperCase()
method in TypeScript, which is used to convert all the characters in a string to uppercase. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use toUpperCase()
effectively can help in various string manipulation tasks in TypeScript, especially when standardizing string cases and performing case-insensitive comparisons.
Comments
Post a Comment
Leave Comment