Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有以下String格式:
String
2.2
我想用空格替换小数点,使其看起来像这样:
22
我该怎么做呢?我以为replace会成功的,但是当我这样尝试时:
replace
string.replace('.', '');
我得到一个错误,''因为它应该不是一个字符。这是有道理的,那么我还能如何完成我想要的呢?
''
如果您只是将单引号交换为双引号,这将起作用,因为空字符串是合法值,而不是“空字符”,并且存在重载replace(CharSequence, CharSequence)。请记住,这CharSequence是String.
replace(CharSequence, CharSequence)
CharSequence
尝试 :
string.replace(".", "");
其他方法是使用replaceAll:
replaceAll
string.replaceAll("\\.","");