我想分开街道、城市、州、国家、邮政编码
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
上面的代码适用于 String "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):街道 = 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): 国家=
3) "zalkhand, 10001, India"
4) Kanaka zalkhand, 10001, India" (, is missing )
像这样
那么如何分离上面的字符串?