替换每个出现的角色,但最后
可以说我有一串abcd
。 如何编写将该字符串转换为abc.d
? 或者有没有任何方法可用的实施?
到目前为止我尝试了什么
int dotPlacing = propertyName.lastIndexOf(".");//12 String modString = propertyName.replace(".", ""); modString = modString.substring(0, dotPlacing-1) + "."+modString.substring(dotPlacing-1);
我正在使用它来编写Hibernate标准。 它适用于user.country.name
但不适用于user.country.name.ss
。 没有尝试任何其他字符串。
你可以将子串forms0提取到lastIndefOf('.')
。 在这个子串中替换所有.
要清空字符串。 与子串合并后(从lastIndexOf .
到end)
String start = "abcdwea.s"; String regex = "\\.(?=.*\\.)"; String end = start.replaceAll(regex, ""); System.out.println(end);
使用StringTokenizer
String in = "abcd"; StringTokenizer t = new StringTokenizer(in,"."); String last = "",result = ""; while(t.hasMoreTokens()) { last = t.nextToken(); result += " "+last; } result = result.trim(); result.replaceAll(last,"."+last);
您在原始字符串上使用的不是dotPlacing
,而是在没有任何点的新字符串上,因此其长度发生了变化,这是您出现问题的主要原因。
将您的代码更改为
int dotPlacing = propertyName.lastIndexOf('.'); String modString = propertyName.substring(0, dotPlacing).replace(".","") + propertyName.substring(dotPlacing); System.out.println(modString);