Android的字符串替换所有不工作 [英] Android String replace All does not work
问题描述
在Android应用程序中,我使用
In Android application, I am using
String num = "10,000,000.00";
num.replaceAll("," , "");
预计:10000000.00
Expected: 10000000.00
不过,我得到:10 000 000.00
But I am getting : 10 000 000.00
适用于大多数的设备。像银河核心,银河大二重奏,银河年轻,Galaxy Note的II与Xperiaç
Works in most of the devices. Facing issue in few devices like Galaxy Core, Galaxy Grand duos, Galaxy Young, Galaxy Note II and Xperia C
请帮助。
推荐答案
的replaceAll 方法不修改 NUM 的价值。相反,它创造了一种新的String对象,并将其返回。因此,所有你需要做的是分配什么的replaceAll是久违的 NUM 变量的值。
replaceAll method is not modifying the num's value. Instead it is creating a new String object and returning it. So all you need to do is assign the value what replaceAll is returning to num variable.
不过,我认为你需要的替换的方法来去除,字符,而不是的replaceAll 其中第一个参数是正则表达式的(让我们不要混在这种情况下,正则表达式) 的。
But I think you need replace method to remove the , characters rather than replaceAll which first parameter is regex (lets not mix the regex in this case).
该医生说:
replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
因此,一切都上升到这样的:
So it all goes up to this:
num = num.replace(",", "");
这篇关于Android的字符串替换所有不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!