Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a String is a number

I know you can check if a string is an integer by just doing Integer.parseInt("1234");
But what I want is to do is link a textual number to an integer.
i.e.

  • "One" == 1
  • "Two" == 2
  • "Three" == 3
  • "Twenty" == 20

Is there some library I can use to do this, or do I have to program this all by hand?

The reason I want to do this, is that I have an android application with speech recognition.
The user can then count. This number is shown on the screen.

EDIT
After some experimenting, I figured out that the SpeechRecognizer class I use automatically parses numbers to actual numbers...

like image 482
Rick Hoving Avatar asked May 04 '13 17:05

Rick Hoving


People also ask

How do you check if a string is a number in C++?

Using built-in method isdigit(), each character of string is checked. If the string character is a number, it will print that string contains int. If string contains character or alphabet, it will print that string does not contain int.

How do you check if a string is an integer?

To check if a string is integer in Python, use the isdigit() method. The string isdigit() is a built-in Python method that checks whether the given string consists of only digits.

How do you check if there is a number in a string Java?

Use the isDigit() Method to Check if String Contains Numbers in Java. To find an integer from a string, we can use this in-built function called isDigit() .

How do you check if a string is a number in Python?

Python String isnumeric() Method The str. isnumeric() checks whether all the characters of the string are numeric characters or not. It will return True if all characters are numeric and will return False even if one character is non-numeric.


2 Answers

I am not sure if there is a library for that but here is a good example.

like image 159
Semih Yagcioglu Avatar answered Oct 04 '22 12:10

Semih Yagcioglu


As far I know there is no library for this but you can think like this

create three types of tokens

a. one, two, three.................... nine

b. eleven to nineteen, twenty, thirty , ............ ninety

c. hundred, thousand, ........ and bigger values

now parse your input string and match with your tokens. Just for a basic idea you can think like this

step 1. create tokens from stirng tokenizer

step 2. match the right most string and match with tokens

step 3. match one by one string from right and match with tokens and calculate

for example ONE THOUSAND FIVE HUNDRED FIFTY THREE

let sum =0

first string = THree so sum +=3

second string = FiFTY so sum += 50;

third string = Hundred so you need to multiply fourth string with 100 and add so sum += f*100.. and so on

This is just a basic idea. So you can implement it perfectly after proper planning

like image 37
stinepike Avatar answered Oct 04 '22 13:10

stinepike