Reputation: 1
How to split this var:
String x = "center=-34.604632,-58.375798";
Into:
float long = "-34.604632";
float lati = "-58.375798";
Upvotes: 0
Views: 90
Reputation: 1
String loc = "center=-34.604632,-58.375798";
//First split by '=', that would give you 'center' and '-34.604632,-58.375798' .Split that again by ','. Then parse it to get a float value
String[] locSplit = loc.split("=")[1].split(",");
float latitude = Float.parseFloat(locSplit[0]);
float longitude = Float.parseFloat(locSplit[1]);
System.out.println(latitude);
System.out.println(longitude);
Upvotes: 0
Reputation: 23644
Split the string by the equals mark:
String x = "center=-34.604632,-58.375798"
String[] xSplit = x.split("=");
Longitude and latitude are in the second half
String longAndLat = xSplit[1];
Split the new string by the comma:
String[] longAndLatSplit = longAndLat.split(",");
Convert the two strings into floats
float longitude = Float.parseFloat(longAndLatSplit[0]);
float latitude = Float.parseFloat(longAndLatSplit[1]);
Upvotes: 2