2

What are the best practices in java for extracting number from text and parse it?

for example:

String s = "Availability in 20 days";

Please don't stick just to the example I am looking for a good general practice and scenarios.

Thank you.

kidwon
  • 4,448
  • 5
  • 28
  • 45
  • if the input is "Number 134 comes 2nd" then what output do you want? – dryairship Feb 12 '16 at 12:29
  • I was thinking on that probably a collection of numbers. But I would rather ignore numbers stick with text – kidwon Feb 12 '16 at 12:30
  • @kidwon please go ahead and accept an answer. or if you were able to come up with some best practices that can help the community, please put in your own answer and accept it. – Yogesh_D Mar 03 '16 at 09:56

5 Answers5

1

Using regular expression:

Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher("Availability in 20 days");
while (m.find()) {
  int number = Integer.parseInt(m.group());
  ...
}
andrucz
  • 1,971
  • 2
  • 18
  • 30
1

How about regex + replaceAll?

code:

String after = str.replaceAll("\\D+", "");
Gary Y Kim
  • 79
  • 4
1

I am not sure of the best practices, but one way that I would do it described in this stack overflow questions.

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

Community
  • 1
  • 1
Yogesh_D
  • 17,656
  • 10
  • 41
  • 55
1

I'm not exactly sure what you want to do, but here are some solutions that might help you:

  1. List item use .indexOf and .substring to find numbers in the string

    • example:

      String s;
      String str = new String("1 sentence containing 5 words and 3 numbers.");
      ArrayList<Integer> integers = new ArrayList<Integer>();
      for (int i = 0; i <= 9; i++) {
          int start = 0;
          while (start != -1) {
              String sub = str.substring(start);
              int x = sub.indexOf(i);
              if (x != -1) {
                  s = sub.substring(x, x+1);
                  integers.add(Integer.parseInt(s));
                  start = x;
              } else {
                  //number not found
                  start = -1;
              }
          }
      }
      
  2. extract one character at a time and try to parse it, if there's no exception, it is a number. I DEFINITELY DON'T RECOMMEND THIS SOLUTION, but it should also work. Unfortunately, I can't tell you which method is quicker, but I can imagine that - although there are fewer commands - the second version is slower, considering that there's several exceptions thrown.

    String s;
    int integ;
    ArrayList<Integer> integers = new ArrayList<Integer>();
    String str = new String("1 sentence containing 5 words and 3 numbers.");
    for (int i = 0; i < str.length(); i++) {
        s = str.substring(i,i+1);
        try {
            integ = Integer.parseInt(s);
            integers.add(integ);
        } catch (NumberFormatException nfe) {
            //nothing
        }
    }
    
PixelMaster
  • 895
  • 10
  • 28
0

if there are serveral numbers then

String[] after = str.replaceAll("\\D+", " ").split("\\s+");
Gary Y Kim
  • 79
  • 4