项目场景:
提示:坐标的逗号与分号分割问题:
问题描述:
坐标分隔的符号不同,进行转换。
预期结果:[145.12939,38.08269];[145.12939,38.281313];[145.43701,38.281313];[145.43701,38.08269];[145.43701,38.08269];[145.43701,38.08269];[145.43701,38.08269];[145.43701,38.08269];[145.43701,38.08269];[145.43701,38.08269]
输入测试数据:[145.12939,38.08269],[145.12939,38.281313],[145.43701,38.281313],[145.43701,38.08269],[145.43701,38.08269],[145.43701,38.08269],[145.43701,38.08269],[145.43701,38.08269],[145.43701,38.08269],[145.43701,38.08269]
解决方案:
/**
* 坐标逗号转分号
*/
public String CommaTosemicolon(String replace) {
replace = replace.replace("]", "").replace("[", "");
String[] coordHandler = replace.split(",");
String[] s = new String[coordHandler.length / 2];
for (int i = 0; i < coordHandler.length; i = i + 2) {
int j = i / 2;
if (j < s.length) {
s[j] = "[" + coordHandler[i] + "," + coordHandler[i + 1] + "]";
}
}
// for (int i = 0; i < s.length; i++) {
// System.out.println(s[i]);
// }
String finallycoord = "";
for (int i = 0; i < s.length; i = i + 1) {
String s1 = "";
s1 = s[i] + ";";
finallycoord = finallycoord + s1;
}
String FinallyString = finallycoord.substring(0, finallycoord.length() - 1);
return FinallyString;
}
拆分后重新拼接。