我有一个地理编码器,gcd和这行代码反向地理编码
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress = (returnedAddress.getAddressLine(i)).toString();
Run Code Online (Sandbox Code Playgroud)
strReturnedAddress返回类似的东西
10453 Central Bronx,New York City,NY
我只需要城市名称,即纽约市.
删除部分字符串将非常困难,因为地理编码的输出可能会发生变化.我只需要地理编码给我这个城市.
我查了http://developer.android.com/reference/android/location/Geocoder.html但找不到答案.
我希望从字符串中删除"是",但如果字符串是,请说
"同位素与物理有关,物理学很有趣"
我只想要替换"is"这个词而不是同位素中的"is".
我可以用
.replaceAll(" is", "")
.replaceAll("is ", "")
Run Code Online (Sandbox Code Playgroud)
但我想用正则表达式来完成这个以供将来参考.
.replaceAll("是"),"")会起作用吗?
谢谢.
String weatherLocation = weatherLoc[1].toString();
weatherLocation.replaceAll("how","");
weatherLocation.replaceAll("weather", "");
weatherLocation.replaceAll("like", "");
weatherLocation.replaceAll("in", "");
weatherLocation.replaceAll("at", "");
weatherLocation.replaceAll("around", "");
test.setText(weatherLocation);
Run Code Online (Sandbox Code Playgroud)
weatherLocation仍然包含"喜欢"
这是代码
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = **gcd.getFromLocation(latitude, longitude, 1);**
Run Code Online (Sandbox Code Playgroud)
出现错误的部分用星号突出显示。
谢谢。