Kumza Ion
Kumza Ion

Reputation: 355

How to split string to extract coordinates

I have a string that contains coordinate "42.262699 N 55.312947 E". I want to remove characters 'N' and 'E' and then split it into two spearate Xcoordinate and Ycoordinate strings. I tried some regex but not able to make it work as I want. Kindly help.

Upvotes: 0

Views: 243

Answers (1)

snachmsm
snachmsm

Reputation: 19243

String cords = "42.262699 N 55.312947 E";
String[] split = str.split("\\s+"); // split by whitespace
//now we have array with every "word", 4 items

double Xcoordinate = Double.parseDouble(split[0]); // parsing to double
double Ycoordinate = Double.parseDouble(split[2]);

String X = split[1]; //"N"
String Y = split[3]; //"E"

you may need to use try{ }catch() for Double.parseDouble( as it may throw parsing exception

Upvotes: 2

Related Questions