2012-03-05 21 views
0

我以“街道邮编城市”的形式收到一个地址(以字符串形式)。 我想要做的是从邮编和城市拆分街道和号码。通常你可以分割空白。但是一些街道名称中也有空格,例如:“Emile Van Ermengemlaan”。所以分割空白是没有选择的。安卓Java分成4个整数

邮政编码总是4个单一数字,例如“1234”,“8560”......我认为这是选项。拆分邮政编码。但我不知道该怎么做。任何帮助?

编辑: 实例:

  1. 实施例1

    “格拉夫卡雷尔德Goedelaan 1 8500 Kortrijk的”=> “格拉夫卡雷尔德Goedelaan 1” 下一行 “8500 Kortrijk的”

  2. 示例2

    “Reigerstraat 24 8930 Lauwe”=>“Reigerstraat 24”next line“8930 Lauwe”

+1

你能举几个例子说明你试图匹配什么吗? – Gray 2012-03-05 14:00:37

+0

对不起,你可以编辑你的问题来展示这些吗? – Gray 2012-03-05 14:32:05

回答

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); 

给出:
格拉夫卡雷尔·德Goedelaan 1 8500科特赖克
道:格拉夫卡雷尔·德Goedelaan 1
邮编:8500
城市:科特赖克

和:
Reigerstraat 24 8930 Lauwe
路:Reigerstraat 24
邮编:8930
市:Lauwe

0

尝试拆分命令解析模式“”,那么你只有parseInt函数

+0

正如你可以在例子中看到的,在串中没有“,” – 2012-03-05 14:31:29

1

那东西未经测试。我在飞行中创建它,可能会有一些错误,但总的想法应该适合。

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); 
1

这是为我工作的代码的例子。它执行这些操作:

  1. 它根据空格分割字符串,并且每个元素都存储在一个字符串数组中;
  2. 它检查数组中每个元素的大小;
  3. 如果大小为== 4(邮政编码的字符数),则它检查(使用模式)当前元素是否为数字;
  4. 如果当前元素是一个数字,那么这个元素和下一个元素(城市)存储在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(); 
        } 
    } 
    

有关串的支票进一步信息,看看这个page,并且对于该ArrayList的Iterator,看this

我希望这可以帮助解决您的问题。