java 简体繁体转换工具包

maven导包

<!-- 简体繁体相互转换 -->
<dependency>
   <groupId>com.spreada</groupId>
   <artifactId>ZHConverter</artifactId>
   <version>1.0.0</version>
</dependency>

由于我没有导成功,就直接下载的点击下载
https://mvnrepository.com/artifact/com.spreada/ZHConverter
下载完成放入项目右键点击Add External Jars
在这里插入图片描述
在这里插入图片描述

package com.crm.common.utils;

import com.spreada.utils.chinese.ZHConverter;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 简体繁体相互转换
 *
 * @author Administrator
 * @createAuthor: lc
 * @createDate: 2023/4/14 9:49
 * @version: 1.0
 */
public class CHSCHTUtils {
    /**
     * 繁体转换简体
     * @return
     */
    public static String familiarStyle(String traditionalSrc){
        ZHConverter converter = ZHConverter.getInstance(ZHConverter.SIMPLIFIED);
        String simplified = converter.convert(traditionalSrc);
        return simplified;
    }

    /**
     * 简体转换繁体
     * @param simplified
     * @return
     */
    public static String complexFont(String simplified){
        return ZHConverter.convert(simplified,ZHConverter.TRADITIONAL);
    }

    /**
     * 遍历实体将简体字改成繁体字
     *
     * @createAuthor: lc
     * @createDate: 2023/4/14 11:54
     * @version: 1.0
     */
    public static <T> List<T> traversalObject(List<T> list) throws InvocationTargetException, IllegalAccessException, IntrospectionException {
        for (T obj : list) {
            Class aClass = obj.getClass();
            Field[] fields = aClass.getDeclaredFields();
            for (Field field : fields) {
                if (field.getType().equals(String.class)) {
                    PropertyDescriptor pd = new PropertyDescriptor(field.getName(),aClass);
                    // 获取get方法
                    Method getMethod = pd.getReadMethod();
                    // 获取set方法
                    Method setMethod = pd.getWriteMethod();
                    // 通过get方法获取到值
                    Object o = getMethod.invoke(obj);
                    if (o != null) {
                        // 更新为新的值
                        setMethod.invoke(obj, complexFont(o.toString()));
                    }
                }
            }
        }
        return list;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值