全角转半角繁转简大写转小写的工具方法

本文介绍了一个Java类CharCodeUtils,用于实现不同字符集之间的转换,包括繁体转简体、简体转繁体及全角转半角等功能。该工具通过加载特定的配置文件来完成转换任务,并提供了一系列实用的方法。
public class CharCodeUtils {
	private static CharsetConverter cc = new CharsetConverter(10);
	private static Logger logger = Logger.getLogger(CharCodeUtils.class);
	private static boolean hasInit = false;

	/**
	 * 获取配置文件的路径 String[]
	 * 
	 * @return
	 * 
	 */
	private static String getFileLoc() throws DomainException {
		String filePath = "";
		try {
			java.net.URL classUrl = CharCodeUtils.class.getResource("");
			filePath = classUrl.getPath();
		} catch (Exception e) {
			logger.error("获取charmap文件绝对路径失败!");
		}
		logger.info("filePath is :"+filePath);
		if (filePath.indexOf(".jar") != -1) {
			String jarFile = filePath.toString().substring(5,
					filePath.toString().length() - 24);
			logger.info("jarFile is :"+jarFile);
			try {
				JarFileOper jfo = new JarFileOper(jarFile);
				return jfo.extraFileToPath();
			} catch (Exception e) {
				logger.error("解压jar包出错!", e);
				throw new DomainException("can't get charmap file from jar:"
						+ jarFile);
			}
		} else {
			return filePath.toString().substring(0,
					filePath.toString().length() - 22)
					+ "charmap";
		}
	}

	/**
	 * 初始化CharCodeUtils,加载相应的编码配置文件 void
	 * 
	 */
	private static void init() throws DomainException {
		if (!hasInit) {
			String path = getFileLoc() + File.separator;
			cc.setFiles(new String[] {
					path + "uct2s.ctb",// 繁体转简体
					path + "ucs2t.ctb",// 简体转繁体
					path + "ucsbc2dbc.ctb",// 全角转半角
					null, null, path + "ucint2ext.ctb", path + "ucrglt.ctb",
					path + "ucphrase.ctb", });
			cc.init();
			hasInit = true;
		}
	}

	/**
	 * 繁体转简体 void
	 * 
	 * @param orString
	 * 
	 */
	public static String t2s(String orString) throws DomainException {
		init();
		return cc.map(0, orString);
	}

	/**
	 * 简体转繁体 void
	 * 
	 * @param orString
	 * 
	 */
	public static String s2t(String orString) throws DomainException {
		init();
		return cc.map(1, orString);
	}

	/**
	 * 全角转半角 void
	 * 
	 * @param orString
	 * 
	 */
	public static String w2h(String orString) throws DomainException {
		init();
		return cc.map(2, orString);
	}

	/**
	 * StringUtils用到的 String
	 * 
	 * @param orString
	 * @return
	 * @throws DomainException
	 * 
	 */
	public static String QBchange(String orString) throws DomainException {
		init();
		//logger.info("QBchange:" + orString);
		return cc.map(2, cc.map(0, orString));

	}

	/**
	 * 转化特殊符号
	 * 
	 * @param str
	 * @return
	 */
	public static String changeSS(String str) {
		str = StringUtils.replace(str, "`", "·");
		str = StringUtils.replace(str, "'", "’");
		str = StringUtils.replace(str, "\"", "”");
		str = StringUtils.replace(str, ",", ",");
		str = StringUtils.replace(str, ";", ";");
		str = StringUtils.replace(str, ".", "。");
		str = StringUtils.replace(str, ":", ":");
		str = StringUtils.replace(str, "?", "?");
		str = StringUtils.replace(str, "!", "!");
		str = StringUtils.replace(str, "\\", "\");
		str = StringUtils.replace(str, "<", "<");
		str = StringUtils.replace(str, ">", ">");

		return str;
	}

	/**
	 * 主方法 void
	 * 
	 * @param argz
	 * 
	 */
	public static void main(String[] argz) throws DomainException {
		QBchange("abc");
	}

}

转载于:https://www.cnblogs.com/highriver/archive/2011/08/24/2152011.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值