Alec Porfiri
Alec Porfiri

Reputation: 1

How to split one var string in two float vars

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

Answers (2)

vincenzo
vincenzo

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

flakes
flakes

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

Related Questions