Java替换方法,替换为空字符 [英] Java replace method, replacing with empty character
问题描述
假设我在字符串
格式中有以下内容:
Suppose I have the following in String
format:
2.2
我想用空格替换小数点,使它看起来像这样:
And I want to replace the decimal point with an empty space, to make it look like this:
22
我该怎么做?我认为替换
会有这个伎俩,但是当我这样尝试时:
How do I do this? I thought replace
would have done the trick, but when I try it like this:
string.replace('。','');
我收到''<的错误/ code>因为它应该不是一个角色。这是有道理的,那么我怎么能完成我想要的呢?
I get an error with the ''
because it supposedly isn't a character. That makes sense, so how else can I accomplish what I want?
推荐答案
如果你只是用双引号换单,这将是因为空字符串是合法值而起作用。似乎许多人忽略了 replace(CharSequence,CharSequence)
实际上对字符串参数起作用的事实:)
If you just exchange single for double quotes, this will work because an empty string is a legal value. It seems that many people overlook the fact that replace(CharSequence, CharSequence)
actually works on string arguments :)
这篇关于Java替换方法,替换为空字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!