🎓 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
Introduction
Java provides several methods to extract characters from a string. These methods are useful for manipulating and analyzing strings at the character level. This tutorial will cover the various methods available for character extraction with examples.
Table of Contents
charAt()getChars()getBytes()toCharArray()- Complete Example Program
- Conclusion
1. charAt()
The charAt() method returns the character at a specified index in a string. The index is zero-based, meaning the first character is at index 0.
Syntax:
public char charAt(int index)
Example:
public class CharAtExample {
public static void main(String[] args) {
String str = "Hello, World!";
char ch = str.charAt(7);
System.out.println("Character at index 7: " + ch);
}
}
Output:
Character at index 7: W
2. getChars()
The getChars() method copies characters from a specified segment of a string into a destination character array.
Syntax:
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
srcBegin: The starting index (inclusive).srcEnd: The ending index (exclusive).dst: The destination array.dstBegin: The starting index in the destination array.
Example:
public class GetCharsExample {
public static void main(String[] args) {
String str = "Hello, World!";
char[] dst = new char[5];
str.getChars(7, 12, dst, 0);
System.out.println("Extracted characters: " + new String(dst));
}
}
Output:
Extracted characters: World
3. getBytes()
The getBytes() method encodes the string into a sequence of bytes using the platform's default charset and returns the resulting byte array.
Syntax:
public byte[] getBytes()
Example:
import java.util.Arrays;
public class GetBytesExample {
public static void main(String[] args) {
String str = "Hello";
byte[] byteArray = str.getBytes();
System.out.println("Byte array: " + Arrays.toString(byteArray));
}
}
Output:
Byte array: [72, 101, 108, 108, 111]
4. toCharArray()
The toCharArray() method converts the string to a new character array.
Syntax:
public char[] toCharArray()
Example:
public class ToCharArrayExample {
public static void main(String[] args) {
String str = "Hello";
char[] charArray = str.toCharArray();
System.out.println("Character array: " + Arrays.toString(charArray));
}
}
Output:
Character array: [H, e, l, l, o]
5. Complete Example Program
Here is a complete program that demonstrates the various character extraction methods discussed above.
Example Code:
import java.util.Arrays;
public class StringCharacterExtraction {
public static void main(String[] args) {
String str = "Hello, World!";
// Using charAt()
char ch = str.charAt(7);
System.out.println("Character at index 7: " + ch);
// Using getChars()
char[] dst = new char[5];
str.getChars(7, 12, dst, 0);
System.out.println("Extracted characters using getChars: " + new String(dst));
// Using getBytes()
byte[] byteArray = str.getBytes();
System.out.println("Byte array using getBytes: " + Arrays.toString(byteArray));
// Using toCharArray()
char[] charArray = str.toCharArray();
System.out.println("Character array using toCharArray: " + Arrays.toString(charArray));
}
}
Output:
Character at index 7: W
Extracted characters using getChars: World
Byte array using getBytes: [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
Character array using toCharArray: [H, e, l, l, o, ,, , W, o, r, l, d, !]
6. Conclusion
Java provides various methods to extract characters from a string, each serving different purposes and use cases. The charAt() method retrieves a single character at a specified index, while the getChars() method copies a range of characters into a destination array. The getBytes() method converts the string into a byte array, and the toCharArray() method converts the string into a character array. Understanding these methods will help you manipulate and analyze strings more effectively in Java.
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
Hi there to everybody, it’s my first go to see of this web site; this weblog consists of awesome and in fact good stuff for visitors. Hurrah, that’s what I was exploring for, what stuff! Existing here at this blog, thanks admin of this web site. You can also visit String Interview Questions for more TutorialCup. related information and knowledge.
ReplyDelete