Android Java在4个整数上拆分
我收到的地址格式为“街道号码邮政编码城市”(字符串形式)。 我想做的是从邮政编码和城市中分割街道和号码。通常,您可以在空白处拆分。但有些街道名称中也有空格,例如:“Emile Van Ermengemlaan”。因此,在空格上拆分是没有选择的 邮政编码始终为4个单号,例如“1234”、“8560”。。。我认为这是一个选择。按邮政编码拆分。但我不知道怎么做。有什么帮助吗 编辑: 示例:Android Java在4个整数上拆分,java,android,string,split,Java,Android,String,Split,我收到的地址格式为“街道号码邮政编码城市”(字符串形式)。 我想做的是从邮政编码和城市中分割街道和号码。通常,您可以在空白处拆分。但有些街道名称中也有空格,例如:“Emile Van Ermengemlaan”。因此,在空格上拆分是没有选择的 邮政编码始终为4个单号,例如“1234”、“8560”。。。我认为这是一个选择。按邮政编码拆分。但我不知道怎么做。有什么帮助吗 编辑: 示例: 例1 “Graaf Karel De Goedelaan 1 8500 Kortrijk”=>“Graaf Ka
尝试使用JGeocoder。尝试使用JGeocoder。尝试使用split命令解析模式“,”,然后只需解析int尝试使用split命令解析模式“,”,然后只需解析int这是未经测试的内容。我在飞行中创建了它,可能会有一些错误,但总体思路应该是合适的
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 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);
给出:格拉夫·卡雷尔·德戈德兰18500科尔特里克
道路:格拉夫·卡雷尔·德戈德兰1号
邮政编码:8500
城市:Kortrijk
和:
赖格斯特拉特24 8930劳韦
道路:Reigerstraat 24号
邮政编码:8930
城市:劳韦
给出:
格拉夫·卡雷尔·德戈德兰18500科尔特里克
道路:格拉夫·卡雷尔·德戈德兰1号
邮政编码:8500
城市:Kortrijk
和:
赖格斯特拉特24 8930劳韦
道路:Reigerstraat 24号
邮政编码:8930
城市:劳韦
这是一个适用于我的代码示例。它执行以下操作:
import java.util.regex.Matcher;
导入java.util.regex.Pattern;
导入java.util.*;
公共阶级分裂{
private List newline=new ArrayList();
公开拆分(){
String myString=“Graaf Karel De Goedelaan 1 8500 Kortrijk”;
字符串数组[]=myString.split(\\s+);
对于(intz=0;z)和,对于ArrayList迭代器,请参见
我希望这有助于解决您的问题。这是一个适用于我的代码示例。它执行以下操作:
它根据空格分割字符串,每个元素存储在字符串数组中
它检查数组中每个元素的大小
如果大小==4(邮政编码的字符数),那么它(使用模式)检查当前元素是否为数字
如果当前元素是一个数字,则此元素和下一个元素(城市)将存储在
import java.util.regex.Matcher;
导入java.util.regex.Pattern;
导入java.util.*;
公共阶级分裂{
private List newline=new ArrayList();
公开拆分(){
String myString=“Graaf Karel De Goedelaan 1 8500 Kortrijk”;
字符串数组[]=myString.split(\\s+);
对于(intz=0;z)和,对于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();
}
}