如何从地址字符串中分离州、城市、邮政编码、国家等?
我想分离街道,城市,州,国家,邮政编码
String = Kanaka, Ranchi, zalkhand, 10001, India
public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StringTokenizer st = new StringTokenizer(" Kanaka, Ranchi, zalkhand, 10001, India");
System.out.println("Tokens are seperated");
int i=0;
String street,city,state,zipcode,country;
while(st.hasMoreTokens())
{
if(i==0)
{
street = st.nextToken(",");
System.out.println("street ="+street);
i++;
}
else if(i==1)
{
city = st.nextToken(",");
System.out.println("city= "+city);
i++;
}
else if(i==2)
{
state = st.nextToken(",");
System.out.println("state ="+state);
i++;
}
else if(i==3)
{
zipcode = st.nextToken(",");
System.out.println("zipcode= "+zipcode);
i++;
}
else if(i==4)
{
contry = st.nextToken(",");
System.out.println("country= "+country);
i++;
}
}
}
}
输出是:
06-23 09:23:37.070: INFO/System.out(435): street = Kanaka
06-23 09:23:37.080: INFO/System.out(435): city= Ranchi
06-23 09:23:37.080: INFO/System.out(435): state = zalkhand
06-23 09:23:37.080: INFO/System.out(435): zipcode= 10001
06-23 09:23:37.080: INFO/System.out(435): country= India
上面的代码可以很好地处理字符串“Kanaka,Ranchi,zalkhand,10001,India”
我的问题是我从 xml 解析地址字符串,这不是很好的格式
ex. 1) "Kanaka, Ranchi, zalkhand, 10001, India"
2) "Ranchi, zalkhand, 10001, India" ---> kanaka(street is absent )
输出: 06-23 09:23:37.070: INFO/System.out(435): 街道 = 兰契 06-23 09:23:37.080: INFO/System.out(435): 城市= zalkhand 06-23 09:23:37.080: 信息/System.out(435): 状态 = 10001 06-23 09:23:37.080: INFO/System.out(435): 邮政编码= 印度 06-23 09:23:37.080:INFO / System.out(435):country =
3) "zalkhand, 10001, India"
4) Kanaka zalkhand, 10001, India" (, is missing )
像这样
那么如何分隔上面的字符串?
I want to separate street, city, state, country, zip code
String = Kanaka, Ranchi, zalkhand, 10001, India
public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StringTokenizer st = new StringTokenizer(" Kanaka, Ranchi, zalkhand, 10001, India");
System.out.println("Tokens are seperated");
int i=0;
String street,city,state,zipcode,country;
while(st.hasMoreTokens())
{
if(i==0)
{
street = st.nextToken(",");
System.out.println("street ="+street);
i++;
}
else if(i==1)
{
city = st.nextToken(",");
System.out.println("city= "+city);
i++;
}
else if(i==2)
{
state = st.nextToken(",");
System.out.println("state ="+state);
i++;
}
else if(i==3)
{
zipcode = st.nextToken(",");
System.out.println("zipcode= "+zipcode);
i++;
}
else if(i==4)
{
contry = st.nextToken(",");
System.out.println("country= "+country);
i++;
}
}
}
}
output is :
06-23 09:23:37.070: INFO/System.out(435): street = Kanaka
06-23 09:23:37.080: INFO/System.out(435): city= Ranchi
06-23 09:23:37.080: INFO/System.out(435): state = zalkhand
06-23 09:23:37.080: INFO/System.out(435): zipcode= 10001
06-23 09:23:37.080: INFO/System.out(435): country= India
Above code is work fine with String "Kanaka, Ranchi, zalkhand, 10001, India"
My problem is I parse address string from xml which is not good format
ex. 1) "Kanaka, Ranchi, zalkhand, 10001, India"
2) "Ranchi, zalkhand, 10001, India" ---> kanaka(street is absent )
output :
06-23 09:23:37.070: INFO/System.out(435): street = Ranchi
06-23 09:23:37.080: INFO/System.out(435): city= zalkhand
06-23 09:23:37.080: INFO/System.out(435): state = 10001
06-23 09:23:37.080: INFO/System.out(435): zipcode= India
06-23 09:23:37.080: INFO/System.out(435): country=
3) "zalkhand, 10001, India"
4) Kanaka zalkhand, 10001, India" (, is missing )
like this
so how to separate above string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以创建一个数组,并在解析地址字符串后使用 StringTokenizer 将标记放入该数组中。但是通过此您将无法区分哪个是城市或邮政编码。
这是相当困难的,所以尝试使 XML 格式良好。例如,如果城市不存在,则设置一个默认值,以便您可以用作不存在的字段。
You can Create a array and after parsing address String put the tokens in that array using StringTokenizer.But via this you won't be able to differentiate which is City or ZIP code.
It is quite difficult so try to make XML well formated.Like if City is absent put a default value so you can use as absent field.
代币和分裂是必要的,但还不够。
您的问题是,如果您遇到多令牌项,您和您的程序都需要足够的上下文来理解一个令牌在哪里结束以及下一个令牌在哪里开始。
美国也存在同样的问题。给定这个地址:
你的大脑有足够的上下文知道“旧金山”是城市,州代码是“CA”,但是你如何告诉计算机程序如何去做呢?
您需要一个更了解地址的解析器。如果分隔符描绘了每个项目的开始和结束位置,将会有所帮助,因此多标记项目不会成为问题。
您可以使用清晰的标记分隔符来解决它:
在“~”处拆分,一切都很好。
Tokens and splitting are necessary but not sufficient.
Your problem is that if you encounter a multi-token item you and your program both need enough context to understand where one token ends and the next begins.
It's the same problem here in the US. Given this address:
Your brain has enough context to know that "San Francisco" is the city, and the state code is "CA", but how do you tell a computer program how to do it?
You need a parser that has more knowledge of what an address is. It would help if the separators delineated where each items started and ended, so multi-token items would not be a problem.
You can solve it with a clear token delimiter:
Split at the "~" and all is well.
您应该创建一个需要五个输入的解析器。如果缺少某些数据,则替换 null
1) "Kanaka, Ranchi, zalkhand, 10001, India"
2) null, "Ranchi, zalkhand, 10001, India" // 如果街道缺失
3) "Kanaka, null, zalkhand, 10001 , India" // 如果缺少城市,
则将此类型的值传递给您的 StringTokenizer。
You should create a parser which should take five inputs. and replace null if some data is missing
1) "Kanaka, Ranchi, zalkhand, 10001, India"
2) null, "Ranchi, zalkhand, 10001, India" // if street is missing
3) "Kanaka, null, zalkhand, 10001, India" // if city is missing
pass this type of value to your StringTokenizer.
正如所指出的,您需要一种正确的方法来解析 XML,其他一切都会很容易。
As pointed out you need a correct way of parsing the XML and all else would be easy.
导入java.util.StringTokenizer;
导入 android.app.Activity;
导入 android.os.Bundle;
mport java.util.StringTokenizer;
import android.app.Activity;
import android.os.Bundle;