how to extract numeric values from input string in java

后端 未结 15 1733
说谎
说谎 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<str.length(); i++)
        if( str.charAt(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<str.length(); i++)
        if( str.charAt(i) > 47 && str.charAt(i) < 58) {
            System.out.print(str.charAt(i));
            flag = true;
        } else {
            System.out.print( flag ? '\n' : "");
            flag = false;
        }
    
    0 讨论(0)
  • 2020-12-13 16:47

    Example using java Scanner class

    import java.util.Scanner;
    
    Scanner s = new Scanner( "abc d 1234567890pqr 54897" );
    s.useDelimiter( "\\D+" );
    while ( s.hasNextInt() ){
        s.nextInt(); // get int
    }
    
    0 讨论(0)
  • 2020-12-13 16:47
    public class ExtractNum
    
    {
    
      public static void main(String args[])
    
      {
    
       String input = "abc d 1234567890pqr 54897";
    
       String digits = input.replaceAll("[^0-9.]","");
    
       System.out.println("\nGiven Number is :"+digits);
    
      }
    
     }
    
    0 讨论(0)
  • 2020-12-13 16:49
     public static String convertBudgetStringToPriceInteger(String budget) {
        if (!AndroidUtils.isEmpty(budget) && !"0".equalsIgnoreCase(budget)) {
            double numbers = getNumericFromString(budget);
            if( budget.contains("Crore") ){
                numbers= numbers* 10000000;
            }else if(budget.contains("Lac")){
                numbers= numbers* 100000;
            }
            return removeTrailingZeroesFromDouble(numbers);
        }else{
            return "0";
        }
    }
    

    Get numeric value from alphanumeric string

     public static double getNumericFromString(String string){
        try {
            if(!AndroidUtils.isEmpty(string)){
                String commaRemovedString = string.replaceAll(",","");
                return Double.parseDouble(commaRemovedString.replaceAll("[A-z]+$", ""));
                /*return Double.parseDouble(string.replaceAll("[^[0-9]+[.[0-9]]*]", "").trim());*/
    
            }
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
        return 0;
    }
    

    For eg . If i pass 1.5 lac or 15,0000 or 15 Crores then we can get numeric value from these fucntion . We can customize string according to our needs. For eg. Result would be 150000 in case of 1.5 Lac

    0 讨论(0)
  • 2020-12-13 16:50
    String str = "abc d 1234567890pqr 54897";
    str = str.replaceAll("[^\\d ]", "");
    

    The result will be "1234567890 54897".

    0 讨论(0)
  • 2020-12-13 16:54

    You can use str = str.replaceAll("replaced_string","replacing_string");

    String str=" abc d 1234567890pqr 54897";
    String str_rep1=" abc d ";
    String str_rep2="pqr ";
    String result1=str.replaceAll("", str_rep1);
    String result2=str.replaceAll(",",str_rep2);
    

    also what npinti suggests is fine to work with.

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