Parsing currency strings in Java
•
Java
Suppose I provide a string similar to "$123456,56.25" or "123'456.67" or something similar (with numbers and decimal points and some separators, or 'or other unpredictable things) I need to write a method that accepts parameters like the above and returns a string, such as "12345656.25" or "123456.67"
Could you please come up with the most effective and readable code to achieve this goal?
Note: I know to traverse each index and check whether its returnrs is character IsDigit (charatinedx) or if (charatinedx = = '.') I am looking for a more optimized solution in terms of efficiency and readability
thank you.
Solution
String newStr = oldStr.replaceAll("[^\\d.]+","")
String newStr = oldStr.replaceAll("[^\\d.]+","")
This will delete any characters that are not numbers or periods
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码