在java中解析货币字符串 [英] Parsing a currency String in java
问题描述
假设我提供的字符串类似于$ 123,456,56.25
或123'456.67
或类似的东西(带有数字和小数点以及某些分隔符,如,
或'
或其他类似的东西是不可预测的)。我需要编写一个方法,它接受类似上面的参数并返回一个字符串,如12345656.25
或123456.67
分别。
Suppose I'm supplied with a String which is like "$123,456,56.25"
or "123'456.67"
or something similar to this (with digits and a decimal point and some seperator like ,
or '
or something else which is not predictable). I need to write a method which takes an argument like the one above and returns a String like "12345656.25"
or "123456.67"
respectively.
你能否提出最有效和最易读的代码来实现这个目标?
Could you please suggest the most efficient and readable code to achieve this?
注意:我知道要检查每个索引并检查其重新定位器 true
是否为 Character.isDigit(charAtInedx)
或 if(charAtInedx =='。')
我正在寻找一个在效率和可读性方面更优化的解决方案
Note: I'm aware of going through each indexes and checking for whether its retunrs true
for Character.isDigit(charAtInedx)
or if(charAtInedx == '.')
I'm looking for a more optimized solution both in terms of efficiency and readability
谢谢。
推荐答案
String newStr = oldStr.replaceAll("[^\\d.]+", "")
这将删除任何字符这不是数字或期间
This will drop any character that is not either a digit or a period
这篇关于在java中解析货币字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!