lat/lng: (51.07459833333334,16.962598333333336)|lat/lng: (51.074299999999994,16.9628)|lat/lng: (51.07749999999999,16.9614)|lat/lng: (51.07770000000001,16.960998333333336)|
我尝试将其转换为LatLng Array。如何拆分这个?
答案 0 :(得分:1)
试试这个:
ids <- as.data.frame(mongoDf$find( fields = '{"Complaint ID":1, "Date received":1, "_id":0}'))
以下是虚拟 // Your string
String strMain = "lat/lng: (51.07459833333334,16.962598333333336)|lat/lng: (51.074299999999994,16.9628)|lat/lng: (51.07749999999999,16.9614)|lat/lng: (51.07770000000001,16.960998333333336)|";
strMain = strMain.replace("lat/lng: (", "");
strMain = strMain.replace(")", "");
String[] strLatLong = strMain.split("\\|");
List<LatLong> latLongList = new ArrayList<LatLong>();
for (String item: strLatLong) {
String[] str = item.split(",");
double latitude = Double.parseDouble(str[0]);
double longitude = Double.parseDouble(str[1]);
latLongList.add(new LatLong(latitude, longitude));
}
// Print List
for (LatLong latLong: latLongList) {
Log.d("LatLong", "Latitude: " + String.valueOf(latLong.getLatitude())
+ "\nLongitude: " + String.valueOf(latLong.getLongitude()));
}
类:
LatLong
<强>输出:强>
public class LatLong {
double latitude;
double longitude;
public LatLong(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
答案 1 :(得分:0)
String input = "lat/lng: (51.07459833333334,16.962598333333336)|lat/lng: (51.074299999999994,16.9628)|lat/lng: (51.07749999999999,16.9614)|lat/lng: (51.07770000000001,16.960998333333336)|";
List<LatLng> list = new ArrayList<>();
String pattern = "lat/lng: \\((.*?),(.*?)\\)\\|";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
int i=0;
while (m.find()) {
double latitude = Double.parseDouble(m.group(1));
double longitude = Double.parseDouble(m.group(2));
list.add(new LatLng(latitude, longitude));
}
// now convert the list to an array
LatLng[] array = list.toArray(new LatLng[0]);
这是一个演示的链接,至少显示正则表达式逻辑正常工作: