简体   繁体   English

Android Java分割为4个整数

[英]Android Java split on 4 integers

I receive an address in the form of "street number postcode city" (as a string). 我收到“街道编号邮政编码城市”形式的地址(作为字符串)。 What I want to do is split the street and number from the postcode and city. 我要做的是从邮政编码和城市中分割街道和数字。 Normally you can split on whitespace. 通常,您可以在空白处分割。 But some streetnames have also whitespace in them, example: "Emile Van Ermengemlaan". 但是有些街道名称中也包含空格,例如:“ Emile Van Ermengemlaan”。 So split on whitespace is no option. 因此,在空白处分割是没有选择的。

The postcode is always 4 single numbers for example "1234","8560", ... And I think this is the option. 邮政编码始终是4个单个数字,例如“ 1234”,“ 8560”,...,我认为这是一个选择。 Split on the postcode. 拆分邮政编码。 But I don't know how to do this. 但是我不知道该怎么做。 Any help? 有什么帮助吗?

EDIT: Examples: 编辑:示例:

  1. Example 1 例子1

    "Graaf Karel De Goedelaan 1 8500 Kortrijk" => "Graaf Karel de Goedelaan 1" next line "8500 Kortrijk" “ Graaf Karel de Goedelaan 1 8500 Kortrijk” =>“ Graaf Karel de Goedelaan 1”下一行“ 8500 Kortrijk”

  2. Example 2 例子2

    "Reigerstraat 24 8930 Lauwe" => "Reigerstraat 24" next line "8930 Lauwe" “ Reigerstraat 24 8930 Lauwe” =>“ Reigerstraat 24”下一行“ 8930 Lauwe”

Thats something untested. 那是未经测试的东西。 I created it on the fly, there might be some mistakes but the general idea should fit. 我是即时创建的,可能会有一些错误,但总体思路应该合适。

String s = "Graaf Karel De Goedelaan 1 8500 Kortrijk";

Pattern p = Pattern.compile("{4}[0-9]");
Matcher m = p.matcher(s);
while(m.find){
    String postcode = m.group(1);
}

// splits around postcode
String[] split = s.split({4}[0-9]);

p = Patter.compile("\D"); //non-digit
m = p.matcher(split[0]);
while(m.find){
    String street = m.group(1);
}


p = Patter.compile("\d"); //digit
m = p.matcher(split[0]);
while(m.find){
    String streetnumber = m.group(1);
}

String city = split[1].substring(1);
String input = "Graaf Karel De Goedelaan 1 8500 Kortrijk";
String[] results = input.split("([0-9]{4})");
String road = results[0].trim();
String city = results[1].trim();
String postcode = "";
Pattern p = Pattern.compile("([0-9]{4})");
Matcher m = p.matcher(input);
while(m.find()) {
    postcode = m.group(1);
}
System.out.println(input);
System.out.println("Road: " + road);
System.out.println("Postcode: " + postcode);
System.out.println("City: " + city);

Gives: 给出:
Graaf Karel De Goedelaan 1 8500 Kortrijk Graaf Karel De Goedelaan 1 8500科特赖克
Road: Graaf Karel De Goedelaan 1 路:Graaf Karel De Goedelaan 1
Postcode: 8500 邮政编码:8500
City: Kortrijk 城市:科特赖克

And: 和:
Reigerstraat 24 8930 Lauwe Reigerstraat 24 8930劳威
Road: Reigerstraat 24 路:Reigerstraat 24
Postcode: 8930 邮政编码:8930
City: Lauwe 城市:劳威

This is an example of code that works for me. 这是一个对我有用的代码示例。 It performs these operations: 它执行以下操作:

  1. It splits the string based on whitespace, and each element is stored on an array of strings; 它根据空格分割字符串,每个元素存储在字符串数组中;
  2. It checks the size of each element of the array; 它检查数组每个元素的大小;
  3. If the size is == 4 (the number of characters of the postal code), then it checks (with a pattern) if the current element is a number; 如果大小为== 4(邮政编码的字符数),则它将检查(带有模式)当前元素是否为数字;
  4. If the current element is a number, then this element and the next element (the city) is stored on an ArrayList . 如果当前元素是数字,则此元素和下一个元素(城市)存储在ArrayList上

     import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.*; public class split{ private List<String> newline = new ArrayList<String>(); public split(){ String myString = "Graaf Karel De Goedelaan 1 8500 Kortrijk"; String array[] = myString.split("\\\\s+"); for(int z = 0;z<arr.length;z++){ int sizestr = array[z].length(); if(sizestr==4){/* if the generic string has 4 characters */ String expression = "[0-9]*"; CharSequence inputStr = array[z]; Pattern pattern = Pattern.compile(expression); Matcher matcher = pattern.matcher(inputStr); if(matcher.matches()){/* then if the string has 4 numbers, we have the postal code" */ newline.add(array[z]); /* now we add the postal code and the next element to an array list" */ newline.add(array[z+1]); } } } Iterator<String> itr = newline.iterator(); while (itr.hasNext()) { String element = itr.next(); System.out.println(element); /* prints "8500" and "Kortrijk" */ } } public static void main(String args[]){ split s = new split(); } } 

For further info about the check of the string, take a look to this page , and, for the ArrayList Iterator, see this . 有关检查字符串的更多信息,请查看此页面 ,对于ArrayList迭代器,请参见this

I hope this can help to solve your problem. 我希望这可以帮助您解决问题。

尝试使用JGeocoder的地址解析器

尝试使用split命令解析模式“,”,则只需解析parseInt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM