Java 中的字符替换

发布于 2024-11-03 22:10:15 字数 421 浏览 0 评论 0原文

我尝试替换字符串中的字符,该字符有时有效,但大多数时候不起作用。

我尝试了以下操作:

String t = "[javatag]";
String t1 = t;
String t2 = t;
t.replace("\u005B", "");
t.replace("\u005D", "");
t1.replace("[", "");
t1.replace("]", "");
t2.replace("\\]", "");
t2.replace("\\[", "");
System.out.println(t+" , "+t1+" , "+t2);

结果输出仍然是“[javatag],[javatag],[javatag]”,而没有替换“[”和“]”。

我应该怎么做才能替换那些“[”和“]”字符?

I tried to replace characters in String which works sometimes and does not work most of the time.

I tried the following:

String t = "[javatag]";
String t1 = t;
String t2 = t;
t.replace("\u005B", "");
t.replace("\u005D", "");
t1.replace("[", "");
t1.replace("]", "");
t2.replace("\\]", "");
t2.replace("\\[", "");
System.out.println(t+" , "+t1+" , "+t2);

The resulting output is still "[javatag] , [javatag] , [javatag]" without the "[" and "]" being replaced.

What should I do to replace those "[" and "]" characters ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

御弟哥哥 2024-11-10 22:10:16

java中的String对象是不可变的。你无法改变它们。

您需要:

t2 = t2.replace("\\]", "");

replace() 返回一个新的 String 对象。

编辑:因为......我要脱离包

既然是这种情况,参数实际上是一个正则表达式,并且你想去掉两个括号,你可以使用 replaceAll() 而不是两个操作:

t2 = t2.replaceAll("[\\[\\]]", "");

这将一次性消除左括号和右括号。

String objects in java are immutable. You can't change them.

You need:

t2 = t2.replace("\\]", "");

replace() returns a new String object.

Edit: Because ... I'm breaking away from the pack

And since this is the case, the argument is actually a regex, and you want to get rid of both brackets, you can use replaceAll() instead of two operations:

t2 = t2.replaceAll("[\\[\\]]", "");

This would get rid of both opening and closing brackets in one fell swoop.

南冥有猫 2024-11-10 22:10:16

字符串是不可变的,因此

t.replace(....);

您不需要

将输出分配给某个变量,例如

t = t.replace(....);

Strings are immutable so

t.replace(....);

does nothing

you need to assign the output to some variable like

t = t.replace(....);
熟人话多 2024-11-10 22:10:16

Java 中的String 是不可变的,这意味着您无法更改它们。相反,请执行 t1 = t1.replace("]", "");。这会将 replace 的结果分配给 t1。

Strings in Java are immutable, meaning you can't change them. Instead, do t1 = t1.replace("]", "");. This will assign the result of replace to t1.

↙厌世 2024-11-10 22:10:16

String.replace 不是这样工作的。您必须使用类似 t = t.replace("t", "")

String.replace doesn't work that way. You have to use something like t = t.replace("t", "")

记忆で 2024-11-10 22:10:16

String.replace() 替换所需字符后返回一个新字符串。因此你需要这样做:

String t = "[javatag]";
t = t.replace("[","");
t = t.replace("]","");

String.replace() returns a new string after replacing the required characters. Hence you need to do it in this way:

String t = "[javatag]";
t = t.replace("[","");
t = t.replace("]","");
各空 2024-11-10 22:10:16
t.replace(....);

给你一个字符串(返回一个字符串),

你可以将原始变量名称重新分配给新字符串

,旧字符串稍后将被垃圾收集
:)

t.replace(....);

gives you a String (return a string)

you can reassign the origin variable name to the new string

and the old string will later been garbage-collected
:)

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