🎓 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
String.toCharArray() method in Java is used to convert a string into a new character array. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
toCharArrayMethod Syntax- Examples
- Converting a String to a Character Array
- Modifying Characters in the Array
- Iterating Over the Character Array
- Conclusion
Introduction
The String.toCharArray() method is a member of the String class in Java. It allows you to create a new character array containing the characters from the string. This method is particularly useful for processing and manipulating individual characters in a string.
toCharArray Method Syntax
The syntax for the toCharArray method is as follows:
public char[] toCharArray()
Examples
Converting a String to a Character Array
The toCharArray method can be used to convert a string into a new character array.
Example
public class ToCharArrayExample {
public static void main(String[] args) {
String message = "Hello, World!";
char[] charArray = message.toCharArray();
System.out.println("Original message: " + message);
System.out.print("Character array: ");
for (char c : charArray) {
System.out.print(c + " ");
}
}
}
Output:
Original message: Hello, World!
Character array: H e l l o , W o r l d !
Modifying Characters in the Array
Once a string is converted to a character array, you can modify individual characters in the array.
Example
public class ToCharArrayExample {
public static void main(String[] args) {
String message = "Hello, World!";
char[] charArray = message.toCharArray();
// Modify characters in the array
charArray[7] = 'J';
charArray[8] = 'a';
charArray[9] = 'v';
charArray[10] = 'a';
String modifiedMessage = new String(charArray);
System.out.println("Original message: " + message);
System.out.println("Modified message: " + modifiedMessage);
}
}
Output:
Original message: Hello, World!
Modified message: Hello, Java!
Iterating Over the Character Array
You can iterate over the character array to process each character individually.
Example
public class ToCharArrayExample {
public static void main(String[] args) {
String message = "Hello, World!";
char[] charArray = message.toCharArray();
System.out.println("Characters in the message:");
for (char c : charArray) {
System.out.println(c);
}
}
}
Output:
Characters in the message:
H
e
l
l
o
,
W
o
r
l
d
!
Conclusion
The String.toCharArray() method in Java is used for converting a string into a character array. By understanding how to use this method, you can efficiently process and manipulate individual characters in your Java applications. Whether you are converting a string to a character array, modifying characters in the array, or iterating over the array, the toCharArray method provides a reliable solution for these tasks.
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