public class Main { private String addSingal(String str) { if (str == null) { return null; } int n = str.length(); if (n == 1) { if (isLetter(str.charAt(0))) return str; return "<>" + str + "<>"; } String res = ""; boolean flag = false;//表示当前字符的前一个字符是否是中文,如果是中文则是true,如果是英文则是false char c; for (int i = 0; i < n; i++) { c = str.charAt(i); if (!isLetter(c)) { if (flag == false) { res = res + "<>" + c; flag = true; } else { res = res + c; flag = true; } } else { if (flag == true) { res = res + "<>" + c; flag = false; } else { res = res + c; flag = false; } } }//for if (!isLetter(str.charAt(n - 1))) return res + "<>";//最后一个字符是中文,末尾需要加<> return res; }//addSingal private boolean isLetter(char c) { if (c - 'a' >= 0 && c - 'z' <= 0) return true; return false; }//isLetter public static void main(String[] args) { System.out.println(new Main().addSingal("abc中国d美国"));//abc<>中国<>d<>美国<> System.out.println(new Main().addSingal("abc中国d美国e"));//abc<>中国<>d<>美国<>e System.out.println(new Main().addSingal("abc中国de"));//abc<>中国<>de System.out.println(new Main().addSingal("abc"));//abc System.out.println(new Main().addSingal("中"));//<>中<> } }
招银中文字符前后加<>
最新推荐文章于 2024-06-15 01:13:53 发布