How to extract numbers from a string and get an array of ints?

后端 未结 13 818
孤城傲影
孤城傲影 2020-11-22 05:32

I have a String variable (basically an English sentence with an unspecified number of numbers) and I\'d like to extract all the numbers into an array of integers. I was wond

相关标签:
13条回答
  • 2020-11-22 06:10

    Using Java 8, you can do:

    String str = "There 0 are 1 some -2-34 -numbers 567 here 890 .";
    int[] ints = Arrays.stream(str.replaceAll("-", " -").split("[^-\\d]+"))
                     .filter(s -> !s.matches("-?"))
                     .mapToInt(Integer::parseInt).toArray();
    System.out.println(Arrays.toString(ints)); // prints [0, 1, -2, -34, 567, 890]
    

    If you don't have negative numbers, you can get rid of the replaceAll (and use !s.isEmpty() in filter), as that's only to properly split something like 2-34 (this can also be handled purely with regex in split, but it's fairly complicated).

    Arrays.stream turns our String[] into a Stream<String>.

    filter gets rid of the leading and trailing empty strings as well as any - that isn't part of a number.

    mapToInt(Integer::parseInt).toArray() calls parseInt on each String to give us an int[].


    Alternatively, Java 9 has a Matcher.results method, which should allow for something like:

    Pattern p = Pattern.compile("-?\\d+");
    Matcher m = p.matcher("There 0 are 1 some -2-34 -numbers 567 here 890 .");
    int[] ints = m.results().map(MatchResults::group).mapToInt(Integer::parseInt).toArray();
    System.out.println(Arrays.toString(ints)); // prints [0, 1, -2, -34, 567, 890]
    

    As it stands, neither of these is a big improvement over just looping over the results with Pattern / Matcher as shown in the other answers, but it should be simpler if you want to follow this up with more complex operations which are significantly simplified with the use of streams.

    0 讨论(0)
  • 2020-11-22 06:11
    Pattern p = Pattern.compile("[0-9]+");
    Matcher m = p.matcher(myString);
    while (m.find()) {
        int n = Integer.parseInt(m.group());
        // append n to list
    }
    // convert list to array, etc
    

    You can actually replace [0-9] with \d, but that involves double backslash escaping, which makes it harder to read.

    0 讨论(0)
  • 2020-11-22 06:11
    public static String extractNumberFromString(String number) {
        String num = number.replaceAll("[^0-9]+", " ");
        return num.replaceAll(" ", "");
    }
    

    extracts only numbers from string

    0 讨论(0)
  • 2020-11-22 06:17

    Extract all real numbers using this.

    public static ArrayList<Double> extractNumbersInOrder(String str){
    
        str+='a';
        double[] returnArray = new double[]{};
    
        ArrayList<Double> list = new ArrayList<Double>();
        String singleNum="";
        Boolean numStarted;
        for(char c:str.toCharArray()){
    
            if(isNumber(c)){
                singleNum+=c;
    
            } else {
                if(!singleNum.equals("")){  //number ended
                    list.add(Double.valueOf(singleNum));
                    System.out.println(singleNum);
                    singleNum="";
                }
            }
        }
    
        return list;
    }
    
    
    public static boolean isNumber(char c){
        if(Character.isDigit(c)||c=='-'||c=='+'||c=='.'){
            return true;
        } else {
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:19
      StringBuffer sBuffer = new StringBuffer();
      Pattern p = Pattern.compile("[0-9]+.[0-9]*|[0-9]*.[0-9]+|[0-9]+");
      Matcher m = p.matcher(str);
      while (m.find()) {
        sBuffer.append(m.group());
      }
      return sBuffer.toString();
    

    This is for extracting numbers retaining the decimal

    0 讨论(0)
  • 2020-11-22 06:19

    The accepted answer detects digits but does not detect formated numbers, e.g. 2,000, nor decimals, e.g. 4.8. For such use -?\\d+(,\\d+)*?\\.?\\d+?:

            Pattern p = Pattern.compile("-?\\d+(,\\d+)*?\\.?\\d+?");
            List<String> numbers = new ArrayList<String>();
            Matcher m = p.matcher("Government has distributed 4.8 million textbooks to 2,000 schools");
            while (m.find()) {  
                numbers.add(m.group());
            }   
            System.out.println(numbers);
    

    Output: [4.8, 2,000]

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