如何删除“ ”来自java字符串

发布于 2024-09-11 06:58:31 字数 224 浏览 2 评论 0原文

我有一个带有 " " 的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

跨年 2024-09-18 06:58:31
cleaned = cleaned.replace("\u00a0","")
cleaned = cleaned.replace("\u00a0","")
感情洁癖 2024-09-18 06:58:31

这是一个两步过程:

strLineApp = strLineApp.replaceAll("&"+"nbsp;", " "); 
strLineApp = strLineApp.replaceAll(String.valueOf((char) 160), " ");

这对我有用。希望对你也有帮助!

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!

贩梦商人 2024-09-18 06:58:31

就像你提到的那样:

String cleaned = s.replace(" "," ");

它对我有用。

The same way you mentioned:

String cleaned = s.replace(" "," ");

It works for me.

明天过后 2024-09-18 06:58:31

有一个现成的解决方案可以从 Apache commons 中取消转义 HTML:

StringEscapeUtils.unescapeHtml("")

如果需要,您也可以转义 HTML:

StringEscapeUtils.escapeHtml("")

There's a ready solution to unescape HTML from Apache commons:

StringEscapeUtils.unescapeHtml("")

You can also escape HTML if you want:

StringEscapeUtils.escapeHtml("")
暗喜 2024-09-18 06:58:31

我们可以进行正则表达式检查并替换 HTML nbsp;

input.replaceAll("[\\s\\u00A0]+$", "") + "");

它会删除输入字符串中的不间断空格。

We can have a regular expression check and replace HTML nbsp;

input.replaceAll("[\\s\\u00A0]+
quot;, "") + "");

It removes non breaking spaces in the input string.

梦旅人picnic 2024-09-18 06:58:31

字符串是不可变的,所以你需要做

string = string.replaceAll(" ","")

Strings are immutable so You need to do

string = string.replaceAll(" ","")
锦爱 2024-09-18 06:58:31

您可以使用JSoup库:

String date = doc.body().getElementsByClass("Datum").html().toString().replaceAll(" ","").trim();

You can use JSoup library:

String date = doc.body().getElementsByClass("Datum").html().toString().replaceAll(" ","").trim();
预谋 2024-09-18 06:58:31

String.replace(char, char) 接受 char 输入(或 CharSequence 输入)

String.replaceAll(String, String) 接受 String 输入并通过正则表达式进行匹配。

例如:

String origStr = "bat";
String newStr = str.replace('a', 'i');
// Now:
// origStr = "bat"
// newStr = "bit"

关键点是返回值包含新编辑的String。调用 replace()/replaceAll() 的原始 String 变量的内容没有更改。

例如:

String origStr = "how are you?";
String newStr = origStr.replaceAll(" "," ");
String anotherStr = origStr.replaceAll(" ","");
// origStr = "how are you?"
// newStr = "how are you?"
// anotherStr = howareyou?"

String.replace(char, char) takes char inputs (or CharSequence inputs)

String.replaceAll(String, String) takes String inputs and matches by regular expression.

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.

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?"
小巷里的女流氓 2024-09-18 06:58:31

我的解决方案如下,只有这个对我有用:

String string = stringWithNbsp.replaceAll("NNBSP", "");

My solution is the following, and only this worked for me:

String string = stringWithNbsp.replaceAll("NNBSP", "");
断桥再见 2024-09-18 06:58:31

Java 中的String 是不可变的。你必须这样做:

String newStr = cleaned.replaceAll(" ", "");

Strings in Java are immutable. You have to do:

String newStr = cleaned.replaceAll(" ", "");
逆夏时光 2024-09-18 06:58:31

我遇到了同样的问题:我需要的元素的内部 HTML 有“ ”,并且我的断言失败了。
由于该问题尚未接受任何答案,但我会建议以下内容,这对我有用

String string = stringwithNbsp.replaceAll("\n", "");

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

String string = stringwithNbsp.replaceAll("\n", "");

P.S : Happy testing :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文