我想)通过正则表达式从字符串末尾删除字符.
例如,如果一个字符串是英国(英国),那么我想替换最后一个)符号.
注意:
1).正则表达式应该只删除最后一个)符号,无论)字符串中存在多少个符号.
请不要使用正则表达式来完成这个简单的任务.
// If the last ) might not be the last character of the String
String s = "Your String with) multiple).";
StringBuilder sb = new StringBuilder(s);
sb.deleteCharAt(s.lastIndexOf(')'));
s = sb.toString(); // s = "Your String with) multiple."
// If the last ) will always be the last character of the String
s = "Your String with))";
if (s.endsWith(")"))
s = s.substring(0, s.length() - 1);
// s = "Your String with)"
Run Code Online (Sandbox Code Playgroud)