In Java, you can convert a String to an array of char values using the toCharArray()
method. Here’s an example:
String str = "Hello World"; char[] charArray = str.toCharArray();
In this example, the toCharArray()
method is called on the str
String object to convert it to an array of char
values, which is then assigned to the charArray
variable.
You can also convert a String to a single char value using the charAt()
method. Here’s an example:
String str = "Hello World"; char firstChar = str.charAt(0);
In this example, the charAt()
method is called on the str
String object to retrieve the first character of the String, which is then assigned to the firstChar
variable.
Java String to char Example: charAt() method
In Java, you can convert a String to a single char using the charAt()
method. Here’s an example:
String str = "Hello World"; char firstChar = str.charAt(0);
In this example, the charAt()
method is called on the str
String object to retrieve the first character of the String, which is then assigned to the firstChar
variable.
You can also convert a String to an array of char values using the toCharArray()
method. Here’s an example:
String str = "Hello World"; char[] charArray = str.toCharArray();
In this example, the toCharArray()
method is called on the str
String object to convert it to an array of char
values, which is then assigned to the charArray
variable.
Java String to char Example: toCharArray() method
In Java, you can convert a String to an array of char values using the toCharArray()
method. Here’s an example:
String str = "Hello World"; char[] charArray = str.toCharArray();
In this example, the toCharArray()
method is called on the str
String object to convert it to an array of char
values, which is then assigned to the charArray
variable.
You can also access individual characters of a String using the charAt()
method. Here’s an example:
String str = "Hello World"; char firstChar = str.charAt(0);
In this example, the charAt()
method is called on the str
String object to retrieve the first character of the String, which is then assigned to the firstChar
variable.