0

How I split a string input in java?

Basically, I want to split the user input and then use each entity seprately.For example if user enter abcd then I want to split each character and then use it as I wish.One way that I knows is using next.charAt() but I could not apply it on long strings having random space between them.

5th Mar 2020, 7:02 AM
Muhammad Bilal
Muhammad Bilal - avatar
3 RĂ©ponses
+ 7
SImple Use split funtion of java String a = "abcd"; String[] arr = a.split("");
5th Mar 2020, 7:18 AM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 3
This is one more way- String str = "abcde"; char[] arr = str.toCharArray();
5th Mar 2020, 8:15 AM
Avinesh
Avinesh - avatar
+ 1
There is one more way using charAt: public class Program { public static void main(String[] args) { String str = "Welcome to Javatpoint portal"; for (int i=0; i<=str.length()-1; i++) { System.out.println("Char at "+i+" place "+str.charAt(i)); } } }
5th Mar 2020, 11:48 AM
ROBIN C R
ROBIN C R - avatar