Home > OS >  How to split string to extract coordinates
How to split string to extract coordinates

Time:11-20

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.

CodePudding user response:

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

  • Related