🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this chapter, we will explore the charCodeAt() method in TypeScript. This method is a built-in function that helps in retrieving the Unicode value of the character at a specified index in a string. Understanding how to use charCodeAt() is useful for manipulating and accessing the Unicode values of characters within strings.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The charCodeAt() method returns the Unicode value of the character at a specified index (position) in a string. If the index is out of range, it returns NaN.
2. Syntax
string.charCodeAt(index);
Parameters
index: A number representing the position of the character you want to retrieve. The index starts from 0.
Return Value
The method returns a number representing the Unicode value of the character at the specified index. If the index is out of range, it returns NaN.
3. Examples
Let's look at some examples to understand how charCodeAt() works in TypeScript.
Example 1: Basic Usage
In this example, we retrieve the Unicode values of characters from a string using valid indexes.
let str: string = "Hello, TypeScript!";
console.log(str.charCodeAt(0)); // Output: 72 (Unicode value of 'H')
console.log(str.charCodeAt(7)); // Output: 84 (Unicode value of 'T')
console.log(str.charCodeAt(15)); // Output: 33 (Unicode value of '!')
Example 2: Index Out of Range
In this example, we use an index that is out of the range of the string length.
let str: string = "Hello, TypeScript!";
console.log(str.charCodeAt(20)); // Output: NaN
Example 3: Iterating Over String
In this example, we iterate over each character in the string and log its Unicode value.
let str: string = "TypeScript";
for (let i = 0; i < str.length; i++) {
console.log(`Unicode value of character at index ${i} (${str.charAt(i)}): ${str.charCodeAt(i)}`);
}
// Output:
// Unicode value of character at index 0 (T): 84
// Unicode value of character at index 1 (y): 121
// Unicode value of character at index 2 (p): 112
// Unicode value of character at index 3 (e): 101
// Unicode value of character at index 4 (S): 83
// Unicode value of character at index 5 (c): 99
// Unicode value of character at index 6 (r): 114
// Unicode value of character at index 7 (i): 105
// Unicode value of character at index 8 (p): 112
// Unicode value of character at index 9 (t): 116
4. Conclusion
In this chapter, we explored the charCodeAt() method in TypeScript, which is used to retrieve the Unicode value of the character at a specified index in a string. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use charCodeAt() effectively can help in various string manipulation tasks in TypeScript, especially when dealing with Unicode values.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment