String to Array in Java – How to Convert Strings to Arrays
String to Array in Java – How to Convert Strings to Arrays
By Shittu Olumide
Explanation:
1. Declare a string variable str and assign the desired string to Donate
Forum
it.
Support our charity and our mission. Donate to freeCodeCamp.org.
2. Use the toCharArray() method on the string str to convert
it into an array of characters. This method splits the string
into individual characters and returns an array containing
those characters.
Output:
H
e
l
l
o
,
W
o
r
l
d
!
string = "Hello,World,How,Are,You?"
delimiter = ","
split_string = string.split(delimiter)
print(split_string)
Explanation:
Output:
Supports
Support regular
our charityexpressions as delimiters,
and our mission. providing flexible
Donate to freeCodeCamp.org.
splitting options.
Example code: Forum Donate
import java.util.StringTokenizer;
**Explanation**:
**Output**:
```java
Hello
World
How
Are
You?
Applications of StringTokenizer Forum Donate
Explanation:
['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
Note: you should choose the manual conversion approach when you
specifically need to perform custom operations during the
conversion process. Otherwise, the built-in methods like
toCharArray() or split() are recommended for simplicity and
efficiency.
5. **split()** :
9. StringTokenizer:
Conclusion
In this article, we discussed various methods to convert a string to
an array in Java.
Happy Coding!
Forum Donate
Support
If you our charity
read this and the
far, thank our author
mission.to
Donate
show to freeCodeCamp.org.
them you care.
Say Thanks
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers,
services, and staff.
Mobile App
Our Charity
About Alumni Network Open Source Shop Support Sponsors Academic Honesty