招银中文字符前后加<>

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("中"));//<>中<>
    }
}
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值