Java regex for remove no digit chracter except decimal point

前端 未结 1 1035
长发绾君心
长发绾君心 2021-01-27 09:53

I have following strings:

S/. 05.56
S/. 0.0
S/. 0.00
S/. 90.10
S/.   01.23
S/.   1.00   
S/.1.80
$/.9.80
$/.  10.80
$/.    89.81
$    89.81

I n

1条回答
  •  再見小時候
    2021-01-27 10:25

    Use ^\\D+ to replace all non-digits from the beginning of each string.

    public static void main(String[] args) {
    
        String[] str = { "S/. 05.56", "S/. 0.0", "S/. 0.00", "S/. 90.10", "S/.   01.23", "S/.   1.00", "S/.1.80",
                "$/.9.80", "$/.  10.80", "$/.    89.81", "$    89.81" };
    
        for (String s : str) {
            System.out.println(s.replaceAll("^\\D+", ""));
        }
    
    }
    

    O/P :

    05.56
    0.0
    0.00
    90.10
    01.23
    1.00
    1.80
    9.80
    10.80
    89.81
    89.81
    

    0 讨论(0)
提交回复
热议问题