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.
Returns a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.
Note: Converts this string to a new character array.
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.
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
Explanation:Here the output comes by creating a char array by using 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
Explanation:Here the output comes by creating a char array by using 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
Explanation:This code prompts the user to enter a string, and then uses the toCharArray() method to convert it into an array of characters called charArray. It then uses a loop to iterate over the characters in the charArray array and count the number of vowels.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment