天天看點

java将日文半角片假名轉成全角的方法

package samples.string.japanese;

public class StringUtil {

  private static final char[] HANKAKU_KATAKANA = { '。', '「', '」', '、', '・',

      'ヲ', 'ァ', 'ィ', 'ゥ', 'ェ', 'ォ', 'ャ', 'ュ', 'ョ', 'ッ', 'ー', 'ア', 'イ',

      'ウ', 'エ', 'オ', 'カ', 'キ', 'ク', 'ケ', 'コ', 'サ', 'シ', 'ス', 'セ', 'ソ',

      'タ', 'チ', 'ツ', 'テ', 'ト', 'ナ', 'ニ', 'ヌ', 'ネ', 'ノ', 'ハ', 'ヒ', 'フ',

      'ヘ', 'ホ', 'マ', 'ミ', 'ム', 'メ', 'モ', 'ヤ', 'ユ', 'ヨ', 'ラ', 'リ', 'ル',

      'レ', 'ロ', 'ワ', 'ン', '゙', '゚' };

  private static final char[] ZENKAKU_KATAKANA = { '。', '「', '」', '、', '・',

      'ヲ', 'ァ', 'ィ', 'ゥ', 'ェ', 'ォ', 'ャ', 'ュ', 'ョ', 'ッ', 'ー', 'ア', 'イ',

      'ウ', 'エ', 'オ', 'カ', 'キ', 'ク', 'ケ', 'コ', 'サ', 'シ', 'ス', 'セ', 'ソ',

      'タ', 'チ', 'ツ', 'テ', 'ト', 'ナ', 'ニ', 'ヌ', 'ネ', 'ノ', 'ハ', 'ヒ', 'フ',

      'ヘ', 'ホ', 'マ', 'ミ', 'ム', 'メ', 'モ', 'ヤ', 'ユ', 'ヨ', 'ラ', 'リ', 'ル',

      'レ', 'ロ', 'ワ', 'ン', '゛', '゜' };

  private static final char HANKAKU_KATAKANA_FIRST_CHAR = HANKAKU_KATAKANA[0];

  private static final char HANKAKU_KATAKANA_LAST_CHAR = HANKAKU_KATAKANA[HANKAKU_KATAKANA.length - 1];

  public static char hankakuKatakanaToZenkakuKatakana(char c) {

    if (c >= HANKAKU_KATAKANA_FIRST_CHAR && c <= HANKAKU_KATAKANA_LAST_CHAR) {

      return ZENKAKU_KATAKANA[c - HANKAKU_KATAKANA_FIRST_CHAR];

    } else {

      return c;

    }

  }

  public static char mergeChar(char c1, char c2) {

    if (c2 == '゙') {

      if ("カキクケコサシスセソタチツテトハヒフヘホ".indexOf(c1) > 0) {

        switch (c1) {

        case 'カ': return 'ガ';

        case 'キ': return 'ギ';

        case 'ク': return 'グ';

        case 'ケ': return 'ゲ';

        case 'コ': return 'ゴ';

        case 'サ': return 'ザ';

        case 'シ': return 'ジ';

        case 'ス': return 'ズ';

        case 'セ': return 'ゼ';

        case 'ソ': return 'ゾ';

        case 'タ': return 'ダ';

        case 'チ': return 'ヂ';

        case 'ツ': return 'ヅ';

        case 'テ': return 'デ';

        case 'ト': return 'ド';

        case 'ハ': return 'バ';

        case 'ヒ': return 'ビ';

        case 'フ': return 'ブ';

        case 'ヘ': return 'ベ';

        case 'ホ': return 'ボ';

        }

      }

    } else if (c2 == '゚') {

      if ("ハヒフヘホ".indexOf(c1) > 0) {

        switch (c1) {

        case 'ハ': return 'パ';

        case 'ヒ': return 'ピ';

        case 'フ': return 'プ';

        case 'ヘ': return 'ペ';

        case 'ホ': return 'ポ';

        }

      }

    }

    return c1;

  }

  private static String B2Q(String QJstr){// 半角-->全角

      String outStr = "";

      String Tstr = "";

      byte[] b = null;

      for (int i = 0; i < QJstr.length(); i++) {

          try {

              Tstr = QJstr.substring(i, i + 1);

              b = Tstr.getBytes("unicode");

          } catch (java.io.UnsupportedEncodingException e) {

              e.printStackTrace();

          }

          if (b[3] == 0) {

              b[2] = (byte) (b[2] - 32);

              b[3] = -1;

              try {

                  outStr = outStr + new String(b, "unicode");

              } catch (java.io.UnsupportedEncodingException e) {

                  e.printStackTrace();

              }

          } else

              outStr = outStr + Tstr;

      }

      return outStr;

  }

  public static String hankakuKatakanaToZenkakuKatakana(String s) {

    if (s.length() == 0) {

      return s;

    } else {

     s = B2Q(s);

     if (s.length() == 1) {

   return hankakuKatakanaToZenkakuKatakana(s.charAt(0)) + "";

     } else {

   StringBuffer sb = new StringBuffer(s);

   int i = 0;

   for (i = 0; i < sb.length() - 1; i++) {

    char originalChar1 = sb.charAt(i);

    char originalChar2 = sb.charAt(i + 1);

    char margedChar = mergeChar(originalChar1, originalChar2);

    if (margedChar != originalChar1) {

     sb.setCharAt(i, margedChar);

     sb.deleteCharAt(i + 1);

    } else {

     char convertedChar = hankakuKatakanaToZenkakuKatakana(originalChar1);

     if (convertedChar != originalChar1) {

      sb.setCharAt(i, convertedChar);

     }

    }

   }

   if (i < sb.length()) {

    char originalChar1 = sb.charAt(i);

    char convertedChar = hankakuKatakanaToZenkakuKatakana(originalChar1);

    if (convertedChar != originalChar1) {

     sb.setCharAt(i, convertedChar);

    }

   }

   return sb.toString();

     }

 }

  }

  public static void main(String[] args) {

    System.out.println(hankakuKatakanaToZenkakuKatakana("aaカ123"));

  }

}