如何在两个浮点数中拆分一个var字符串

时间:2016-02-24 19:10:16

标签: java

如何拆分此var:

String x = "center=-34.604632,-58.375798";

分为:

float long = "-34.604632";
float lati = "-58.375798";

2 个答案:

答案 0 :(得分:2)

用等号标记分割字符串:

String x = "center=-34.604632,-58.375798"
String[] xSplit = x.split("=");

经度和纬度在下半年

String longAndLat = xSplit[1];

用逗号分隔新字符串:

String[] longAndLatSplit = longAndLat.split(",");

将两个字符串转换为浮点数

float longitude = Float.parseFloat(longAndLatSplit[0]);
float latitude = Float.parseFloat(longAndLatSplit[1]);

答案 1 :(得分:0)

 String loc = "center=-34.604632,-58.375798";

//首先用'='分割,这会给你'中'和'-34.604632,-58.375798'。再次用','分裂。然后解析它以获得浮点值

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);