如何删除“ ”来自java字符串
我有一个带有 " "
的 java 字符串,该字符串来自程序使用 Buffered Reader 对象访问的文本文件。我尝试过 string.replaceAll(" ","") 但它似乎不起作用。
有什么想法吗?
cleaned = cleaned.replace(" "," ");
I have a java string with " "
from a text file the program accesses with a Buffered Reader object. I have tried string.replaceAll(" ","")
and it doesn't seem to work.
Any ideas?
cleaned = cleaned.replace(" "," ");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这是一个两步过程:
这对我有用。希望对你也有帮助!
This is a two step process:
This worked for me. Hope it helps you too!
就像你提到的那样:
它对我有用。
The same way you mentioned:
It works for me.
有一个现成的解决方案可以从 Apache commons 中取消转义 HTML:
如果需要,您也可以转义 HTML:
There's a ready solution to unescape HTML from Apache commons:
You can also escape HTML if you want:
我们可以进行正则表达式检查并替换 HTML
nbsp;
它会删除输入字符串中的不间断空格。
We can have a regular expression check and replace HTML
nbsp;
It removes non breaking spaces in the input string.
字符串是不可变的,所以你需要做
Strings are immutable so You need to do
您可以使用JSoup库:
You can use JSoup library:
String.replace(char, char)
接受char
输入(或CharSequence
输入)String.replaceAll(String, String)
接受String
输入并通过正则表达式进行匹配。例如:
关键点是返回值包含新编辑的
String
。调用replace()
/replaceAll()
的原始String
变量的内容没有更改。例如:
String.replace(char, char)
takeschar
inputs (orCharSequence
inputs)String.replaceAll(String, String)
takesString
inputs and matches by regular expression.For example:
The key point is that the return value contains the new edited
String
. The originalString
variable that invokesreplace()
/replaceAll()
doesn't have its contents changed.For example:
我的解决方案如下,只有这个对我有用:
My solution is the following, and only this worked for me:
Java 中的
String
是不可变的。你必须这样做:String
s in Java are immutable. You have to do:我遇到了同样的问题:我需要的元素的内部 HTML 有“ ”,并且我的断言失败了。
由于该问题尚未接受任何答案,但我会建议以下内容,这对我有用
P.S:快乐测试:)
I encountered the same problem: The inner HTML of the element I needed had " " and my assertion failed.
Since the question has not accepted any answer,yet I would suggest the following, which worked for me
P.S : Happy testing :)