替换Java中的字符 [英] Replacing characters in Java
本文介绍了替换Java中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了以下操作:
String t =[javatag];
String t1 = t;
String t2 = t;
t.replace(\\\[,);
t.replace(\\\],);
t1.replace([,);
t1.replace(],);
t2.replace(\\],);
t2.replace(\\ [,);
System.out.println(t +,+ t1 +,+ t2);
结果输出仍然是 [javatag],[javatag]
我应该怎么做来替换那些[和] 字符?
解决方案
字符串是不可变的
<
t.replace(....);
不执行任何操作
输出到一些变量如
t = t.replace(....);
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 ?
解决方案
Strings are immutable so
t.replace(....);
does nothing
you need to assign the output to some variable like
t = t.replace(....);
这篇关于替换Java中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文