toCharArray() method java

https:/‮ww/‬w.lautturi.com
toCharArray() method java

The toCharArray() method in Java is a method of the String class that converts a string into an array of characters.

Here's an example of how you can use the toCharArray() method to convert a string into an array of characters:

String str = "hello";
char[] chars = str.toCharArray();

In this example, the toCharArray() method is called on the str string and the resulting array of characters is stored in the chars array.

The toCharArray() method does not take any arguments and returns an array of char elements that represent the characters in the string. The size of the returned array is equal to the length of the string.

You can use the toCharArray() method to perform operations on the characters in a string, such as sorting the characters or searching for a specific character. You can also use the toCharArray() method to pass a string as an array of characters to a method that expects an array of char as an argument.

It's important to note that the toCharArray() method creates a new array of characters and does not modify the original string. If you want to modify the characters in a string, you can use the charAt() method to access individual characters and the getChars() method to copy a range of characters from a string into an array.

Created Time:2017-10-17 20:18:57  Author:lautturi