Question

I want to have a int from String when the String have a symbol in the EditText. How i get it?.

for example:

when input in EditText like Rp.1.000.000.000, and i will get an integer with value 1000000000. please, answer my question in java language. Thanks in advance.

Was it helpful?

Solution

In this case (when the token defining it as a number is at the beginning):

final String input = "Rp.1.000.000.000";

int    value = 0;
String token = "Rp";

if (input.contains(token))
    value = Integer.parseInt(input.replaceAll("[^012456789]", ""));



Explanation

.replaceAll("[^012456789]", "") removes all non-numeric chars (R, P and . in this case). So the leftover is a String only containing digits which can easily be converted into an int by using Integer.parseInt()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top