how to extract numeric values from input string in java

后端 未结 15 2053
说谎
说谎 2020-12-13 16:25

How can I extract only the numeric values from the input string?

For example, the input string may be like this:

String str=\"abc d 1234567890pqr 548         


        
15条回答
  •  难免孤独
    2020-12-13 16:46

    Just extract the digits

    String str=" abc d 1234567890pqr 54897";        
    
    for(int i=0; i 47 && str.charAt(i) < 58)
            System.out.print(str.charAt(i));
    

    Another version

    String str=" abc d 1234567890pqr 54897";        
    boolean flag = false;
    for(int i=0; i 47 && str.charAt(i) < 58) {
            System.out.print(str.charAt(i));
            flag = true;
        } else {
            System.out.print( flag ? '\n' : "");
            flag = false;
        }
    

提交回复
热议问题