我需要替换带分隔符的字符串中的子字符串。比如(abc),(def)反转后应该是(cba),(fed)。
我尝试了以下代码,但它没有反转就返回了字符串。
String s = "(abc),(cdef)";
s = s.replaceAll("\\(\\[.*?\\]\\)",
new StringBuilder("$1").reverse().toString());
最佳答案
替代方案:
String s = "(abc),(cdef),(ghij)", res = "";
Matcher m = Pattern.compile("\\((.*?)\\)").matcher(s);
while(m.find()){
res += "(" + new StringBuilder(m.group(1)).reverse().toString() + "),";
}
if(res.length() > 0)
res = res.substring(0,res.length()-1);
System.out.println(res);
打印:
(cba),(fedc),(jihg)
关于java - 反转分隔符之间的多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34619021/