+ 1
A string question.
How do you divide a String sentence into different words? For ex."Hello My name is" to "Hello" "My" and so on.
6 Réponses
+ 2
Mayur Gowda
String[] arr = str.split(" "); //assume there is space.
for(int i = 0; i < arr.length(); i++) {
System.out.println(arr[i]);
}
You can access using index.
+ 2
Mayur Gowda Because string contains space so we need to pass space in split method. There can be any special character in String.
Your String can be like this also:
Hello_My_Name_is
Or
Hello@My@Name@is
It helps in that case when we want to send data with key value pair from UI side to Backend using JSON.
+ 1
Mayur Gowda Split the string into String array.
+ 1
I Am Groot ! Oh ok. Thanks a lot. Much appreciated.
0
I Am Groot ! Using the split method? I tried it, but I can't access the individual words.
0
I Am Groot ! Ok Thanks It worked, but can you tell me why the space makes a difference?