Java String Manipulation: extracting integer and float from string based on pattern

后端 未结 2 539
我在风中等你
我在风中等你 2021-01-07 03:42

I have the following two possible contents of a String. Obviously the amounts always vary and I would like to extract the key information and

Case 0:   pri         


        
2条回答
  •  北海茫月
    2021-01-07 04:18

    That looks like a job for regex:

    String pricesString = "10+: $1.46 100+: $0.16 500+: $0.04";
    Pattern p = Pattern.compile("(\\d+)\\+: \\$(\\d\\.\\d\\d)");
    Matcher m = p.matcher(pricesString);
    while (m.find()) {
        Intger.parseInt(m.group(1));
        Double.parseDouble(m.group(2));
    }
    

    You can choose between the 3 cases by a simple .length() check. The code above is for the last case. The rest is eaiser

提交回复
热议问题