Total Pageviews

2011/07/29

字串半型轉全型

Problem
使用者希望所輸入的地址,在存入資料庫的時候,能夠將數字的部份轉成全型,這樣列印起來比較美觀。

Solution
1:  package gov.fdc.nig.web.util;  
2:  import org.apache.commons.lang.StringUtils;  
3:  /**  
4:  * 用於處理NIG系統內部字串  
5:  *  
6:  * @author albert  
7:  *  
8:  */  
9:  public class NIGStringUtils {  
10:       private static NIGStringUtils instance = new NIGStringUtils();  
11:       public static NIGStringUtils getInstance(){  
12:            return instance;  
13:       }  
14:       /**  
15:       * 將地址半型的數字轉成全型數字  
16:       *  
17:       * @param str String  
18:       * @return  
19:       */  
20:       public String covertToChineseFullChar(String str) {  
21:            String result = "";  
22:            if (StringUtils.isNotEmpty(str)) {  
23:                 char[] chars = str.toCharArray();  
24:                 for (int i = 0; i < chars.length; i++) { //bypass Chinese character   
25:                      if (chars[i] > '\200') {  
26:                           continue;  
27:                      }  
28:                      // 半型空白轉成全型空白  
29:                      if (chars[i] == 32) {  
30:                           chars[i] = (char) 12288;  
31:                           continue;  
32:                      }  
33:                      // 有定義的字、數字及符號  
34:                      if (Character.isLetterOrDigit(chars[i])) {  
35:                           chars[i] = (char) (chars[i] + 65248);  
36:                           continue;  
37:                      }  
38:                      // 其它不合要求的,全部轉成全型空白。  
39:                      chars[i] = (char) 12288;  
40:                 }  
41:                 result = String.valueOf(chars);  
42:            }  
43:            return result;  
44:       }  
45:  }  


}



No comments: