如何删除“ ” 来自java字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3318404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove " " from java string
提问by Greg
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.
我有一个 java 字符串,其中" "
包含程序使用缓冲读取器对象访问的文本文件。我已经尝试过了string.replaceAll(" ","")
,但似乎不起作用。
Any ideas?
有任何想法吗?
cleaned = cleaned.replace(" "," ");
回答by Manuel Selva
The same way you mentioned:
与您提到的方式相同:
String cleaned = s.replace(" "," ");
It works for me.
这个对我有用。
回答by Damian Leszczyński - Vash
Strings are immutable so You need to do
字符串是不可变的,所以你需要做
string = string.replaceAll(" ","")
回答by JohnB
String.replace(char, char)
takes char
inputs (or CharSequence
inputs)
String.replace(char, char)
接受char
输入(或CharSequence
输入)
String.replaceAll(String, String)
takes String
inputs and matches by regular expression.
String.replaceAll(String, String)
String
通过正则表达式获取输入和匹配。
For example:
例如:
String origStr = "bat";
String newStr = str.replace('a', 'i');
// Now:
// origStr = "bat"
// newStr = "bit"
The key point is that the return value contains the new edited String
. The original String
variable that invokes replace()
/replaceAll()
doesn't have its contents changed.
关键是返回值包含新编辑的String
. String
调用replace()
/的原始变量replaceAll()
的内容没有改变。
For example:
例如:
String origStr = "how are you?";
String newStr = origStr.replaceAll(" "," ");
String anotherStr = origStr.replaceAll(" ","");
// origStr = "how are you?"
// newStr = "how are you?"
// anotherStr = howareyou?"
回答by raf
String
s in Java are immutable. You have to do:
String
Java 中的 s 是不可变的。你必须要做:
String newStr = cleaned.replaceAll(" ", "");
回答by Nitin Phadnis
cleaned = cleaned.replace("\u00a0","")
回答by michdraft
回答by Deep Sehgal
This is a two step process:
这是一个两步过程:
strLineApp = strLineApp.replaceAll("&"+"nbsp;", " ");
strLineApp = strLineApp.replaceAll(String.valueOf((char) 160), " ");
This worked for me. Hope it helps you too!
这对我有用。希望对你也有帮助!
回答by RichardK
There's a ready solution to unescape HTML from Apache commons:
有一个现成的解决方案可以从 Apache 公共资源中转义 HTML:
StringEscapeUtils.unescapeHtml("")
You can also escape HTML if you want:
如果需要,您还可以转义 HTML:
StringEscapeUtils.escapeHtml("")
回答by Stroev
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
我遇到了同样的问题:我需要的元素的内部 HTML 有“ ”并且我的断言失败。由于该问题尚未接受任何答案,但我会建议以下内容,这对我有用
String string = stringwithNbsp.replaceAll("\n", "");
P.S : Happy testing :)
PS:快乐测试:)