我需要帮助来解析Java中的字符串...我是Java的新手,我不知道如何去做。
假设我要解析的字符串是......
String str =" NC43-EB2; 49.21716; -122.667252; 49.216757; -122.666235;"
我想要做的是:
字符串名称= C43
字符串方向= EB2;
然后我想做的就是将2个坐标作为一对存储......
坐标c1 = 49.21716; -122.667252; 坐标c2 = 49.216757; -122.666235;
然后创建一个List来存储c1和c2。
到目前为止,我有这个:
parseOnePattern(String str){
String toParse = str;
name = toParse.substring(1, toParse.indexOf("-")); direction = toParse.substring(toParse.indexOf("-", toParse.indexOf(";")));
我不确定如何前进。任何帮助将不胜感激。
答案 0 :(得分:0)
String str3 = "NC43-EB2;49.21716;-122.667252;49.216757;-122.666235;";
String sub = str3.substring(0,4); // sub = NC43
String sub4 = str3.substring(5,9); // sub = EB2;
HashMap<String, String> hm = new HashMap<>();
hm.put(str3.substring(9 ,30), str3.substring(30));
hm.forEach((lat, lot) -> {
System.out.println(lat + " - " + lot); // 49.21716;-122.667252; - 49.216757;-122.666235;
});
//edit if using an array non pairs (I assumed it was lat + lon)
List<String> coordList = new ArrayList<>();
coordList.add(str3.substring(9 ,30));
coordList.add(str3.substring(30));
coordList.forEach( coord -> {
System.out.println(coord);
});
//output : 49.21716;-122.667252;
49.216757;-122.666235;
答案 1 :(得分:0)
欢迎使用Java及其允许在字符串上执行的整个操作集。你有一整套要执行的操作,我会给你一些代码来执行它们并让你开始: -
public void breakString() {
String str = "NC43-EB2;49.21716;-122.667252;49.216757;-122.666235";
// Will break str to "NC43-EB2" and "49.21716" "-122.667252" "49.216757" "-122.666235"
String [] allValues = str.split(";", -1);
String [] nameValuePair = allValues[0].split("-");
// substring selects only the specified portion of string
String name = nameValuePair[0].substring(1, 4);
// Since "49.21716" is of type String, we may need it to parse it to data type double if we want to do operations like numeric operations
double c1 = 0d;
try {
c1 = Double.parseDouble(allValues[1]);
} catch (NumberFormatException e) {
// TODO: Take corrective measures or simply log the error
}
我建议您阅读String类的文档,了解有关字符串拆分和将一种数据类型转换为另一种数据类型的操作的更多信息,并使用像Eclipse这样具有非常有用功能的IDE。此外,我还没有测试上面的代码,所以使用它作为参考而不是模板。
答案 2 :(得分:0)
一个简单的子字符串函数可以解决您的问题。
String str = "NC43-EB2;49.21716;-122.667252;49.216757;-122.666235;"; String[]s = str.split(";"); String[]n = s[0].split("-"); String name = n[0].substring(1); String direction = n[1]; String c1 = s[1] +";"+s[2]; String c2 = s[3] +";"+s[4]; System.out.println(name + " " + direction); System.out.println(c1 + " " + c2);
我希望这会对你有所帮助。
答案 3 :(得分:0)
好的,我做了这个:
public static void main(String[] args) {
String str = "NC43-EB2;49.21716;-122.667252;49.216757;-122.666235;";
String[] strSplit = str.split(";");
String[] nameSplit=strSplit[0].split("-");
String name=nameSplit[0].replace("N", "");
String direction= nameSplit[1];
String cordanateOne = strSplit[1]+";"+strSplit[2]+";";
String cordanateTwo = strSplit[3]+";"+strSplit[4]+";";
System.out.println("Name: "+name);
System.out.println("Direction: "+direction);
System.out.println("Cordenate One: "+cordanateOne);
System.out.println("Cordenate Two: "+cordanateTwo);
}
Name: C43
方向:EB2 Cordenate One:49.21716; -122.667252; Cordenate Two:49.216757; -122.666235;