• java 非法字符过滤 , 半角/全角替换


    java 非法字符过滤 , 半角/全角替换

    package mjorcen.netty.test1;
    
    import java.io.UnsupportedEncodingException;
    
    public class CharByteConverter {
    
        /**
         * 全角转半角
         * 
         * @param str
         * @return
         * 
         * @author mjorcen
         * @email mjorcen@gmail.com
         * @dateTime Sep 27, 2014 2:51:50 PM
         * @version 1
         */
        @Deprecated
        public static final String toSingleByte(String str) {
            StringBuffer outStrBuf = new StringBuffer("");
    
            String Tstr = "";
            byte[] b = null;
            for (int i = 0; i < str.length(); i++) {
                Tstr = str.substring(i, i + 1);
                // 全角空格转换成半角空格
                if (Tstr.equals(" ")) {
                    outStrBuf.append(" ");
                    continue;
                }
                try {
                    b = Tstr.getBytes("unicode");
                    // 得到 unicode 字节数据
                    if (b[2] == -1) {
                        // 表示全角
                        b[3] = (byte) (b[3] + 32);
                        b[2] = 0;
                        outStrBuf.append(new String(b, "unicode"));
                    } else {
                        outStrBuf.append(Tstr);
                    }
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            } // end for.
            return outStrBuf.toString();
    
        }
    
        /**
         * 半角转全角
         * 
         * @param str
         * @return
         * 
         * @author mjorcen
         * @email mjorcen@gmail.com
         * @dateTime Sep 27, 2014 2:52:06 PM
         * @version 1
         */
        @Deprecated
        public static final String toDoubleByte(String str) {
            StringBuffer outStrBuf = new StringBuffer("");
            String Tstr = "";
            byte[] b = null;
            for (int i = 0; i < str.length(); i++) {
                Tstr = str.substring(i, i + 1);
                if (Tstr.equals(" ")) {
                    // 半角空格
                    outStrBuf.append(Tstr);
                    continue;
                }
                try {
                    b = Tstr.getBytes("unicode");
                    if (b[2] == 0) {
                        // 半角
                        b[3] = (byte) (b[3] - 32);
                        b[2] = -1;
                        outStrBuf.append(new String(b, "unicode"));
                    } else {
                        outStrBuf.append(Tstr);
                    }
                    return outStrBuf.toString();
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            }
            return outStrBuf.toString();
        }
    
        /**
         * 半角转全角
         * 
         * @param str
         * @return
         * 
         * @author mjorcen
         * @email mjorcen@gmail.com
         * @dateTime Sep 27, 2014 2:52:31 PM
         * @version 1
         */
        public static String ToSBC(String str) {
            char c[] = str.toCharArray();
            for (int i = 0; i < c.length; i++) {
                if (c[i] == ' ') {
                    c[i] = 'u3000';
                } else if (c[i] < '177') {
                    c[i] = (char) (c[i] + 65248);
    
                }
            }
            return new String(c);
        }
    
        /**
         * 全角转半角
         * 
         * @param str
         * @return
         * 
         * @author mjorcen
         * @email mjorcen@gmail.com
         * @dateTime Sep 27, 2014 2:52:50 PM
         * @version 1
         */
        public static String ToDBC(String str) {
            char c[] = str.toCharArray();
            for (int i = 0; i < c.length; i++) {
                if (c[i] == 'u3000') {
                    c[i] = ' ';
                } else if (c[i] > 'uFF00' && c[i] < 'uFF5F') {
                    c[i] = (char) (c[i] - 65248);
    
                }
            }
            String returnString = new String(c);
            return returnString;
        }
    
        public static void main(String[] args) {
            long l = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                String str = "814乡道阿斯蒂芬123/.12,412看2家1快2看了就2;看了2叫看来+看来家12考虑就23;了3接口2了2会2,.水电费苦辣时间的2  125123123121209-021~!@#$%^&*()_";
                String result = ToDBC(str);
                ToSBC(result);
            }
            System.out.println(System.currentTimeMillis() - l);
        }
    
    }
  • 相关阅读:
    JQuery
    a:hover伪类在ios移动端浏览器内无效的解决方法
    git rebase 过程中遇到冲突该怎么解决?
    git
    vue单文件中scoped样式如何穿透?
    微信小程序-怎么获取当前页面的url
    es6 class 中 constructor 方法 和 super
    如何将svg图标快速转换成字体图标?
    JavaWeb三大组件之一Filter知识总结
    Session的引入以及Cookie的不足
  • 原文地址:https://www.cnblogs.com/mjorcen/p/3996397.html
Copyright © 2020-2023  润新知