在Java中解析字符串,然后存储在变量中

在Java中解析字符串,然后存储在变量中,java,arrays,string,parsing,Java,Arrays,String,Parsing,我需要帮助来解析Java中的字符串。。。我对Java非常陌生,不知道该怎么做 假设我要分析的字符串是 String str=“NC43-EB2;49.21716;-122.667252;49.216757;-122.666235;” 我想做的是: 字符串名称=C43 串方向=EB2 然后我要做的是将两个坐标成对存储… 坐标c1=49.21716-122.667252; 坐标c2=49.216757-122.666235; 然后列出存储c1和c2的列表。 到目前为止,我有: parseOnePat

我需要帮助来解析Java中的字符串。。。我对Java非常陌生,不知道该怎么做

假设我要分析的字符串是

String str=“NC43-EB2;49.21716;-122.667252;49.216757;-122.666235;”

我想做的是:

字符串名称=C43

串方向=EB2

然后我要做的是将两个坐标成对存储…

坐标c1=49.21716-122.667252; 坐标c2=49.216757-122.666235;

然后列出存储c1和c2的列表。

到目前为止,我有:

parseOnePattern(字符串str){

字符串toParse=str

    name = toParse.substring(1, toParse.indexOf("-"));
    direction = toParse.substring(toParse.indexOf("-", toParse.indexOf(";")));
我不知道如何前进。任何帮助都将不胜感激。

String str3=“NC43-EB2;49.21716;-122.667252;49.216757;-122.666235;”;
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;
String sub=str3.substring(0,4);//sub=NC43 String sub4=str3.substring(5,9);//sub=EB2; HashMap hm=新的HashMap(); hm.put(str3.子串(9,30),str3.子串(30)); hm.forEach((横向,纵向)->{ System.out.println(lat+“-”+批次);//49.21716;-122.667252;-49.216757;-122.666235; }); //如果使用非对数组(我假设它是lat+lon),则编辑 List coordList=新建ArrayList(); 添加(str3.子字符串(9,30)); 添加(str3.子字符串(30)); coordList.forEach(coord->{ 系统输出打印(coord); }); //产量:49.21716;-122.667252; 49.216757;-122.666235;
欢迎使用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,它具有非常有用的功能。此外,我还没有测试上述代码,所以将其用作参考,而不是模板。

简单的子字符串函数这可能会解决你的问题

我希望这对你有帮助。

好的,我做了这个:

   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一号:49.21716;-122.667252;
Cordenate Two:49.216757;-122.666235;

根据数据的一致性,您可能需要研究正则表达式。这个问题与JSON有什么关系?您的字符串不是JSON格式,否则您将使用JSON解析器API:对不起,与JSON无关,我已经编辑了我的答案……如果我想开发一个正则表达式若要将每个坐标成对存储,我将如何执行此操作?我注意到,每秒钟的坐标都以“-”开头,这可能会有所帮助。我认为要分离每个坐标,我将使用字符串[]result=s.split(“;”);看起来你在下面通过拆分得到了一些不错的答案,如果它们解决了你的问题,那么你就很好(确保将它们标记为已解决)。如果您对启动正则表达式模式并在实现之前进行实验感兴趣,请结帐。我喜欢该网站。如果您需要Java正则表达式教程,您可以通过Google找到很多。我将有更多坐标数据,而不仅仅是给定的4个值,因此我认为对于大量数据bcuz-yo来说,实现这一点并不好你正在为substring方法指定特定的值…你认为呢?这取决于你的字符串是否具有相同的长度。我假设它们具有相同的长度。数据量没有问题,因为你可以编写自己的方法,这将进行拆分。然后为每个字符串循环它。谢谢-这非常有用…但是-1参数termean在split方法中(我知道这是限制,但这是如何确定的?)另外,我不确定在代码中的什么地方指定了如何解析坐标…(是的,你是对的,我必须将每个字符串坐标解析为双精度)。这是文档中的直接引用-limit参数控制应用模式的次数,因此会影响结果数组的长度。如果f n为非正,则将尽可能多地应用模式,并且数组可以有任何长度。如果n为零,则将尽可能多地应用模式这是可能的,数组可以有任何长度,尾随的空字符串将被丢弃。至于坐标,Java中没有内置的坐标类。有一个名为Points的模糊类,但我认为您不希望这样。您必须创建自己的自定义坐标类,可能使用lat/long作为成员变量,并存储t它们有两个值。请尝试在Java中查找类
   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