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

后端 未结 2 562
我在风中等你
我在风中等你 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:24

    Apply the regex \d+\.?\d* as often as you can. The array of the results can be checked whether it contains 0, 1 or more values.

    If there are 0, it is Case 0.

    If there is one, it is Case 1. You can edd it with qunantity 1 to the articles.

    If there are more, you can loop with something like

    for(int i = 0; i < result.length / 2; i++) {
        articles.addArticle(Integer.parseInt(result[i]), Double.parseDouble(result[i+1]));
    }
    

提交回复
热议问题