Java 将字符串中的英文括号转成中文括号

喜欢ヅ旅行 2023-10-07 23:53 82阅读 0赞
  1. @Test
  2. public void mysqlTest6() throws Exception {
  3. String a="AdEFg(Hi)";
  4. String b="31007(689)";
  5. System.out.println(replaceStr(a));
  6. System.out.println(replaceStr(b));
  7. }
  8. // 将英文括号替换成中文括号
  9. private String replaceStr(final String str) {
  10. String newNode = null;
  11. String allConvertNode = null;
  12. // 替换英文括号的时候因为正则表达式问题,英文前括号和后括号的前边都要加上\\
  13. if (str.contains("(") && str.contains(")")) {
  14. newNode = str.replaceAll("\\(", "(");
  15. allConvertNode = newNode.replaceAll("\\)", ")");
  16. } else if (str.contains("(") && !str.contains(")")) {
  17. allConvertNode = str.replaceAll("\\(", "(");
  18. } else if (!str.contains("(") && str.contains(")")) {
  19. allConvertNode = str.replaceAll("\\)", ")");
  20. } else {
  21. allConvertNode = str;
  22. }
  23. return allConvertNode;
  24. }

输出结果:

AdEFg(Hi)
31007(689)

发表评论

表情:
评论列表 (有 0 条评论,82人围观)

还没有评论,来说两句吧...

相关阅读