🎓 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
StringBuffer.codePointAt() method in Java is used to return the Unicode code point at a specified index within the StringBuffer object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
codePointAtMethod Syntax- Examples
- Getting Code Point at a Specific Index
- Handling Out of Bounds
- Conclusion
Introduction
The codePointAt() method is a member of the StringBuffer class in Java. It allows you to retrieve the Unicode code point value of the character at a specified position within the StringBuffer. This is useful for working with characters outside the Basic Multilingual Plane (BMP), which require two char values (a surrogate pair) to be represented.
codePointAt Method Syntax
The syntax for the codePointAt method is as follows:
public synchronized int codePointAt(int index)
Parameters:
index- an integer specifying the index of the character whose Unicode code point is to be returned.
Returns:
- The Unicode code point value of the character at the specified index.
Throws:
IndexOutOfBoundsException- if theindexargument is negative or not less than the length of this sequence.
Examples
Getting Code Point at a Specific Index
The codePointAt method can be used to access the Unicode code point at a particular index in a StringBuffer object.
Example
public class StringBufferCodePointAtExample {
public static void main(String[] args) {
// Create a StringBuffer object with initial content
StringBuffer sb = new StringBuffer("Hello, World!");
// Get the Unicode code point at index 7
int codePoint = sb.codePointAt(7);
// Print the Unicode code point
System.out.println("Unicode code point at index 7: " + codePoint);
// Print the character at index 7
System.out.println("Character at index 7: " + (char) codePoint);
}
}
Output:
Unicode code point at index 7: 87
Character at index 7: W
Handling Out of Bounds
Attempting to access an index outside the bounds of the StringBuffer will result in an IndexOutOfBoundsException.
Example
public class StringBufferCodePointAtExample {
public static void main(String[] args) {
// Create a StringBuffer object with initial content
StringBuffer sb = new StringBuffer("Hello");
try {
// Attempt to get the Unicode code point at an out-of-bounds index
int codePoint = sb.codePointAt(10);
} catch (IndexOutOfBoundsException e) {
// Handle the exception
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Error: String index out of range: 10
Conclusion
The StringBuffer.codePointAt() method in Java provides a way to retrieve the Unicode code point value of the character at a specified index within a StringBuffer object. By understanding how to use this method, you can work with Unicode characters, including those outside the Basic Multilingual Plane, more effectively. This method is particularly useful for applications that need to handle a wide range of characters and symbols in various languages.
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