⬅ Previous Topic
Java Stringsubstring()
methodNext Topic ⮕
Java StringtoLowerCase()
methodtoCharArray()
method⬅ Previous Topic
Java Stringsubstring()
methodNext Topic ⮕
Java StringtoLowerCase()
methodThe toCharArray()
method in Java is a handy tool for transforming a String into an array of characters. Think of it like taking a sentence (the String) and breaking it down into individual letters (characters) that you can then work with more directly.
public char[] toCharArray()
Parameter | Description |
---|---|
N/A | This method doesn't accept any parameters. It simply converts the String itself into a character array. |
The toCharArray()
method returns a new char[]
(character array) representing the sequence of characters in this string.
This example demonstrates the simplest use case – converting a String into its character array representation.
public class Main {
public static void main(String[] args) {
String str = "Hello";
char[] charArray = str.toCharArray();
for (char c : charArray) {
System.out.print(c);
}
}
}
Hello
Explanation: We create a String str and then call the toCharArray()
method on it, storing the result in a character array named charArray. The loop iterates through each character of the charArray and prints it to the console.
This example shows how you can modify the resulting character array, which directly impacts the original String's representation (although Strings are immutable in Java – modifications create a new String).
public class Main {
public static void main(String[] args) {
String str = "Java";
char[] charArray = str.toCharArray();
charArray[0] = 'j'; // Convert to lowercase
charArray[1] = 'a';
String newStr = new String(charArray);
System.out.println("Original String: " + str);
System.out.println("Modified String: " + newStr);
}
}
Original String: Java
Modified String: java
Explanation: We convert the string “Java” to a character array. Then we change the first and second elements of this charArray to lowercase. Note that changing the charArray doesn't modify the original str, because strings are immutable in Java. A new String instance is created using the modified charArray.
This example illustrates how you can use the character array to perform operations that might be easier on individual characters than working directly with the String object.
public class Main {
public static void main(String[] args) {
String str = "12345";
char[] charArray = str.toCharArray();
for (int i = 0; i < charArray.length; i++) {
if (Character.isDigit(charArray[i])) {
charArray[i] = Character.toUpperCase(charArray[i]);
}
}
String modifiedStr = new String(charArray);
System.out.println("Original String: " + str);
System.out.println("Modified String: " + modifiedStr);
}
}
Original String: 12345
Modified String: 12345
Explanation: The code converts the string “12345” into a character array. It iterates through each element of this charArray and checks if the current element is a digit using Character.isDigit()
. If it's a digit, it converts the digit to upper case by calling Character.toUpperCase()
. Finally, creates a new String from the modified character array.
This example showcases how to handle Unicode characters correctly using toCharArray()
.
public class Main {
public static void main(String[] args) {
String str = "你好世界"; // Hello World in Chinese
char[] charArray = str.toCharArray();
for (char c : charArray) {
System.out.print(c);
}
}
}
你好世界
Explanation: This demonstrates that toCharArray()
handles Unicode characters correctly, ensuring each character is represented accurately in the resulting array.
⬅ Previous Topic
Java Stringsubstring()
methodNext Topic ⮕
Java StringtoLowerCase()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.