I need to extract a substring from a string, the length of the string is unknown. For example "435323 London KingRoad" and I need to extract London and Kingroad (each of them separately) How can I do it?
Asked
Active
Viewed 120 times
0
-
2"the length of the string is unknown" - but you can always find the length of the original string, with the `length()` method. Are you really just looking for the `split()` method though? – Jon Skeet Mar 09 '22 at 19:50
-
Does this answer your question? https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java?r=SearchResults&s=1|443.4187 – blurfus Mar 09 '22 at 19:50
-
use split inside of substring – Med Elgarnaoui Mar 09 '22 at 19:51
-
Yes, the link to the question you sent me helped me a lot, thank u so much! – Mimi Mar 09 '22 at 20:41
1 Answers
0
If every string is in this format, you might as well just write
String[] words = string.split(" ");
return new CityAndRoad(words[1], words[2]);
or return them in whatever other format is appropriate.

Louis Wasserman
- 191,574
- 25
- 345
- 413