番地、都市、州、国、郵便番号を分けたい
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: INFO/System.out(435): 状態 = 10001 06-23 09:23:37.080: INFO/System.out(435): zipcode= インド 06-23 09:23:37.080: INFO/System.out (435): 国=
3) "zalkhand, 10001, India"
4) Kanaka zalkhand, 10001, India" (, is missing )
このような
では、上記の文字列を分離する方法は?