A string question. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

15th Dec 2020, 5:39 PM
Mayur Gowda
Mayur Gowda - avatar
6 Answers
+ 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.
15th Dec 2020, 5:51 PM
A͢J
A͢J - avatar
+ 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.
15th Dec 2020, 5:59 PM
A͢J
A͢J - avatar
+ 1
Mayur Gowda Split the string into String array.
15th Dec 2020, 5:40 PM
A͢J
A͢J - avatar
+ 1
I Am Groot ! Oh ok. Thanks a lot. Much appreciated.
15th Dec 2020, 6:01 PM
Mayur Gowda
Mayur Gowda - avatar
0
I Am Groot ! Using the split method? I tried it, but I can't access the individual words.
15th Dec 2020, 5:41 PM
Mayur Gowda
Mayur Gowda - avatar
0
I Am Groot ! Ok Thanks It worked, but can you tell me why the space makes a difference?
15th Dec 2020, 5:53 PM
Mayur Gowda
Mayur Gowda - avatar