如何用Java替换字符串中的点(。) [英] How to Replace dot (.) in a string in Java
本文介绍了如何用Java替换字符串中的点(。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串叫 persons.name
我想要替换DOT 。
带 / * /
即我的输出将是 persons / * / name
I want to replace the DOT .
with /*/
i.e my output will be persons/*/name
我试过这段代码:
String a="\\*\\";
str=xpath.replaceAll("\\.", a);
我收到StringIndexOutOfBoundsException。
I am getting StringIndexOutOfBoundsException.
如何我要替换点吗?
推荐答案
在点之前需要两个反斜杠,一个用于逃避斜线以便它通过,并且另一个是逃避点,所以它变成字面意思。正斜杠和星号按字面值处理。
You need two backslashes before the dot, one to escape the slash so it gets through, and the other to escape the dot so it becomes literal. Forward slashes and asterisk are treated literal.
str=xpath.replaceAll("\\.", "/*/"); //replaces a literal . with /*/
这篇关于如何用Java替换字符串中的点(。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文