我使用Geocoder獲取地址。有時我會得到兩次城市或郵政編碼。我想刪除這些重複的單詞以使地址更易於接受。有沒有辦法做到這一點?我搜索刪除字符串中的重複單詞,但只有一個很好的result但它沒有在我的情況。從如果我取我的當前地址下面的代碼,我得到的結果是:刪除地址中的重複單詞
Street no 0360
DH Block(Newtown), Action Area I, Newtown
New Town, West Bengal 700156
New Town
700156
India
代碼
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
String result = null;
try {
addresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
} catch (IOException ioException) {
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " +"Latitude = " + location.getLatitude() +", Longitude = " + location.getLongitude(), illegalArgumentException);
}
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
deliverResultToReceiver(AppUtils.LocationConstants.FAILURE_RESULT, errorMessage, null);
} else {
Address address = addresses.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append(" ");
}
sb.append(address.getLocality()).append(" ");
sb.append(address.getPostalCode()).append(" ");
sb.append(address.getCountryName());
result = sb.toString();
}
請解釋爲何鏈接的問題無效。這正是如何刪除重複並保持順序。將所有元素添加到LinkedHashSet –
我試過但我仍然得到相同的結果。 – CodeAssasins
你不清楚你試過了什麼。此外,它唯一應該處理的價值是「DH Block(Newtown),Action Area I,Newtown」,但只有當你在空格分割並在Block之後加一個時,然後去掉括號。換句話說,我認爲你不知道如何使這個答案適應你的代碼。你的預期產出是多少? –