分享

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

 十三笑兮兮 2012-07-10

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

 

http://www.cnblogs.com/highriver/archive/2011/08/24/2152011.html

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");

    }

 

}

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约