天天看點

字元串特殊字元全角半角轉換

String sourceFileName = "\\/:*?\"<>|.你好!";
	System.out.println("source value=====>" + sourceFileName);
	
	//字元串轉全角
	String tmp = "";
    for (int i = 0; i < sourceFileName.length(); i++) {
        if (sourceFileName.charAt(i) == 32) {
            tmp = tmp + String.valueOf((char)(12288));
        }else if (sourceFileName.charAt(i) < 127) {
            tmp = tmp + String.valueOf((char)(sourceFileName.charAt(i) + 65248));
        }else
            tmp = tmp + String.valueOf((char)(sourceFileName.charAt(i)));
    }
    System.out.println("toFull after value=====>" + tmp);


	//字元串轉半角
    String tmp2 = "";
    for (int i = 0; i < tmp.length(); i++) {
        if (tmp.charAt(i) > 65280 && tmp.charAt(i) < 65375) {
            tmp2 += String.valueOf((char)(tmp.charAt(i) - 65248));
        }
        else if (tmp.charAt(i) == 12288) {
            tmp2 += String.valueOf((char)(32));
        }
        else {
            tmp2 += String.valueOf((char)(tmp.charAt(i)));
        }
    }
    System.out.println("toHalf after value=====>" + tmp2);

	執行結果如下:
	source value=====>\/:*?"<>|.你好!
	toFull after value=====>\/:*?"<>|.你好!
	toHalf after value=====>\/:*?"<>|.你好!