Java String toCharArray() Method
What is string in java?
In Java, A string is a sequence of characters, typically used to store and manipulate text. In Java, a String is a class that represents a string of characters. In programming languages, strings are often used to represent text that the user enters or text that is displayed to the user.
- Strings are used frequently in Java programs to represent text, such as user input, program output, and data stored in text files.
- Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.
To know more about String Class in java read the complete article.
String toCharArray() Method:
The Java toCharArray() method in Java is a method of the String class that converts a String object into an array of characters.The toCharArray() method takes no arguments and returns an array of char values. Each element in the array represents a character in the original string.
- The toCharArray() method is useful when you need to work with the individual characters in a string, rather than the string as a whole. For example, you might use it to reverse the characters in a string, or to sort the characters in a string alphabetically.
The toCharArray() method takes no arguments and returns an array of char values. Each element in the array represents a character in the original string.
Syntax:
public char[] toCharArray()
Throws:
The toCharArray() method does not throw any exceptions.
Let’s look at a string-related Java program where the Java String toCharArray() Method is used to perform an operation on the given string.
Example 1 : Java String tocharArray () Method
public class Main { public static void main(String[] args) { String str = "Welcome to PrepInsta"; char[] result; result = str.toCharArray(); System.out.println(result); } }
Output
Welcome to PrepInsta
Example 2 : Java string tocharArray Method
public class Main { public static void main(String args[]) { String s = "PrepInsta Prime"; char[] main = s.toCharArray(); for (int i = 0; i < main.length; i++) { System.out.println(main[i]); } } }
Output
P r e p I n s t a P r i m e
Example 3: Java String tocharArray Method
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a string: "); String str = scanner.nextLine(); // Count the number of vowels in the string using the toCharArray() method char[] charArray = str.toCharArray(); int vowelCount = 0; for (char c : charArray) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') { vowelCount++; } } System.out.println("Number of vowels: " + vowelCount); } }
Output
Enter a string:a Number of vowels: 1
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment