-1

I would like to know is there any java function to extract all numbers from string. String examples:

"Preostalo stanje u Zicer tarifi: 248 min i 0 sec, 497 SMS, 220 MB. Tarifa vrijedi do 01.06.2015." I would like to extract 284 and 0, 497, and 220 and date.

Same function should extract other types of string like "Trenutacno imas 326 bonus MB koji ti vrijede do 02.06.2015." Where it would extract 326 and date.

String like "Imas jos 42:39 od 50 minuta razgovora. Imas jos 81 od 150 poruka. Imas jos 118,0176MB. Nemas aktiviranu opciju 1000 bonbon minuta i SMS-ova :(" ect.

I tought I can make function to walk trough string until it finds numerical char then it would phrase it, but that seems complicates.

5
  • You might be able to use regular expressions; depending on how many different "kinds" of input you are dealing with. And then I have bad news for your: parsing text is complicated. And typically, complexity needs to be addressed. So don't be surprised if you end up coding something complicated in the end. Commented May 26, 2015 at 12:55
  • Do you want the extracted numbers to be separated? Commented May 26, 2015 at 12:55
  • It's not complicate, wow, just a little small method... Commented May 26, 2015 at 12:56
  • So apparently homeworks done in Yahoo answers are asked here now ? Commented May 26, 2015 at 12:56
  • 1
    Simplest solution is to replace all non-numeric characters with spaces and then split values based on spaces Commented May 26, 2015 at 12:56

1 Answer 1

1

Your best bet in this case would be a regular expression combined with a conversion from String to Integer. So in your case, I would do the following:

  1. Read up on regular expressions and write a regular expression that matches a number.
  2. Get all matches of that regular expression with the string that you want to extract the number from.
  3. Convert the matched substring into an integer. Luckily, there already is a method that does exactly that.
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe this pattern can be used: \d+(,?\d+)?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.