2012-03-05 23 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

我希望這可以幫助解決您的問題。