i want to ask something, i already get the number from string and put it inside string using replaceAll like this:
String y = "9.86-A5.321";
String x = y.replaceAll("\D+","");
textview.setText(x);
Now my question is i need to make the number i get inside my String x (9865321) into something like this:
9865321
9000000
800000
60000
5000
300
20
1
How do i make it in Android Studio with Java?
2
Answers
This is not tested, but the concept can be something like this:
The concept is iterate over your digit string, create a new string that will be the digit you want + 0’s and create a copy of original digits, starting from the digit you want and onward.
You have the length of the original number string, so you can iterate over every character of that string and add as many zeros to it as the difference between the string length and which position the character occupies in it:
This is the trivial way to do it. You should use a
StringBuilder
forstring2
instead, and you can probably optimize the two loops somehow, but the above code produces the correct output: