2017-02-08 46 views
-1

我已經從地理編碼器獲取用戶城市,州和國家。在此格式如下如何使用StringBuilder格式化此格式

D/ProfileFrg: StateName: Pune, Maharashtra 411053 
D/ProfileFrg: CountryName: India 

我想格式化使用StringBuilder的成單行字符串:浦那,馬哈拉施特拉邦,印度其郵政編碼將被刪除。

我沒有使用StringBuilder之前,我是新來的任何人都可以幫助解決這個問題嗎?

+0

http://docs.oracle.com/javase/tutorial/java/data/buffers.html – Phil3992

+0

爲什麼你認爲你應該使用'StringBuilder'?而不是說'Formatter'?或'String.replace' {,'All'}? –

+0

@AndyTurner如何在我的情況下使用它?要刪除郵政編碼? – Contextioner

回答

0
A better snippet would be as below. 

String abc = "D/ProfileFrg: StateName: Pune, Maharashtra 411053" + "D/ProfileFrg: CountryName: India"; 
     /* split results with below output. 
     * D/ProfileFrg StateName Pune, Maharashtra 411053D/ProfileFrg 
     * CountryName India 
     */ 
     String[] x = abc.split(":"); 

     String y = x[2] + ", " + x[4]; 
     /* y is Pune, Maharashtra 411053D/ProfileFrg, India */ 

     String[] z = y.split(" "); 
     /* z has Pune, Maharashtra 411053D/ProfileFrg, 
     * India 
     */ 
     String finalValue = z[1] + " " + z[2] + ", " + z[5]; 
     System.out.println(finalValue); 
+0

java.lang.ArrayIndexOutOfBoundsException:length = 1; index = 2 @ String y = x [2] +「,」+ x [4]; – Contextioner

+0

請注意,我拆分abc文本是字符串abc =「D/ProfileFrg:StateName:Pune,Maharashtra 411053」+「D/ProfileFrg:CountryName:India」; – Prashanth

+0

D/ProfileFrg:StateName:Pune,Maharashtra 411053 and D/ProfileFrg:國家名稱:印度 – Prashanth

0

那麼我找到了解決方案來替換字符串中的數字。

String place = stateName + countryName; 
     place = place.replaceAll("[0-9]",""); 
     Log.d(TAG, place);