获取字符串里面的浮点型和整形的数据

获取字符串里面的浮点型和整形的数据
 public static void main(String[] args){
    
    
        String s="房租:2189元bai 水费du:112.9元 电费:569元 物业费:832元";

        String regex="[0-9]+(\\.[0-9]+)?";

        float sum=0;

        Pattern p = Pattern.compile(regex);

        Matcher m = p.matcher(s);

        while(m.find()){
    
    
            if(m.group().contains(".")){
    
    
                System.out.println("浮点型="+m.group());;

            }else{
    
    
                System.out.println("整数="+m.group());;

            }

        }

    }

猜你喜欢

转载自blog.csdn.net/Insect_boy/article/details/109739726