I'm learning Java and I need to split a String into 2 parts. For example I get the address field from database and it looks like this: "Piazza Victoria 3425" and I need to split this into 2 fields, the first is streetName and contains "Piazza Victoria" and the second is streetNo and contains "3485". How can I do that? Thanks in advance!
-
what have you tried? It seems you don't have problems with implementation, just with logic – Stultuske Feb 02 '21 at 12:31
-
1Does this answer your question? [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – AP11 Feb 02 '21 at 12:37
-
1Will the streetNo always consist of a single word (i.e., some bunch of characters not containing any spaces) and will it always appear at the end of the address? It's fairly easy if both of those conditions are true, possibly a lot more difficult if not. – Kevin Anderson Feb 02 '21 at 12:40
-
@Kevin Yes both conditions are true – elvis Feb 02 '21 at 12:41
3 Answers
Split it by the first number in String
and trim the space at the end of streetName
. If you wouldn't trim the string, then the result would be "Piazza Victoria "
.
String str = "Piazza Victoria 3425";
String[] part = str.split("(?<=\\D)(?=\\d)");
String streetName = part[0].trim();
int streetNo = Integer.parseInt(part[1]);
EDIT:
If the streetNo
doesn't have to be number, then split it by spaces and build the streetName
.
String[] part = str.split(" ");
StringBuilder sb = new StringBuilder(part[0]);
for(int i = 1; i < part.length-1; i++) {
sb.append(" ").append(part[i]);
}
String streetName = sb.toString();
String streetNo = part[part.length-1];

- 617
- 4
- 11
-
according to the comments, the number can contain non-numerical chars. so why not just string split with space as regex, the last element is the number, and concatenate the rest to the street? – Stultuske Feb 02 '21 at 12:49
-
@AP11 Instead of int streetNo = Integer.parseInt(part[1]) can I do that String streetNo = part[1]? – elvis Feb 02 '21 at 14:28
-
@elvis of course, you can. I didn't know whether you want to use it as number or as text – AP11 Feb 02 '21 at 14:37
There is a way to split the address by space. check the link below for more info
str = "Hello, This is a String";
String[] splited = str.split("\\s+");
https://stackoverflow.com/a/7899558/7904389
But the good practice is to save the address as street name, street number in different field so that you can get it later.

- 301
- 2
- 16
-
don't just post a link as an answer, post an actual answer, you might add the link as "additional info", and, saving it in different fields is the actual goal here. If you just want to answer it by the answer of that so question, you should have flagged it as a duplicate – Stultuske Feb 02 '21 at 12:42
-
It seems you want to split a string into text and digits. You'll need to pass in a regex to the String.split() method in Java to efficiently do this.
String[] parts = "Piazza Victoria 123".split("(?<=\\D)(?=\\d)");
String streetName = parts[0].trim();
String streetNo = parts[1];
You can check out this link to find out more Need to split a string into two parts in java.
I hope it is helpful

- 96
- 6
-
why not provide an example here? if you just wanted to point to that so answer, you should flag it as a duplicate – Stultuske Feb 02 '21 at 12:43
-
-
@dreamcrash problem is, by writing answers that get downvoted, it 'll take even longer before they can. – Stultuske Feb 02 '21 at 12:48
-
@Stultuske Yep, I agree with that, I am just saying that even if e.unix wanted e.unix cannot do it xD – dreamcrash Feb 02 '21 at 12:50
-