java 全角、半角字符串转换

转自:http://www.cnblogs.com/modou/articles/2679815.html     加入了空字符串的验证

 

半角转全角的方法:

   /** 
    * @Title: ToSBC
    * @Description: 半角转全角
    * @param input String.
    * @return 全角字符串.
    * @throws 
    */
    public static String ToSBC(String input) {
        if(!("".equals(input)||input == null||input.length()<=0)){
            char c[] = input.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);
        }else{
            return input;
        }
    }


全角转半角的方法:

   /** 
     * @Title: ToDBC
     * @Description: 全角转半角
     * @param input String.
     * @return 半角字符串.
     * @throws 
     */    
    public static String ToDBC(String input) {
        if(!("".equals(input)||input == null||input.length()<=0)){
            char c[] = input.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);
    
              }
            }
            return new String(c);
        }else{
            return input;
        }
    }  
    


 

 

posted @ 2014-06-26 17:03  江湖危险快点跑  阅读(409)  评论(0编辑  收藏  举报