Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string at first occurrence of an integer in a string

Tags:

java

string

I have a bunch of Strings in this format:

Barry's Restaurant 100 East Main Street

And I want to split the string into two parts:

Barry's Restaurant 
100 East Main Street

So, I need to split the string at the first occurrence of an integer (the street address). Any help is appreciated, thanks

like image 660
Harry Avatar asked Dec 19 '22 07:12

Harry


1 Answers

If I understand your question, you could do it with something like

public static void main(String[] args) {
    String line = "Barry's Restaurant 100 East Main Street";
    String[] arr = line.split("\\d+", 2);
    String pt1 = arr[0].trim();
    String pt2 = line.substring(pt1.length() + 1).trim();
    System.out.println(pt1);
    System.out.println(pt2);
}

Output is

Barry's Restaurant
100 East Main Street
like image 179
Elliott Frisch Avatar answered Mar 15 '23 00:03

Elliott Frisch