Created
May 7, 2014 04:08
-
-
Save amano41/8b8191e462db28c8cb16 to your computer and use it in GitHub Desktop.
全角⇔半角の変換
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 半角 ⇒ 全角 | |
*/ | |
String halfToFull(String value) { | |
StringBuilder sb = new StringBuilder(value); | |
for (int i = 0; i < sb.length(); i++) { | |
int c = (int) sb.charAt(i); | |
if ((c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A)) { | |
sb.setCharAt(i, (char)(c + 0xFEE0)); | |
} | |
} | |
return sb.toString(); | |
} | |
/** | |
* 全角 ⇒ 半角 | |
*/ | |
String fullToHalf(String value) { | |
StringBuilder sb = new StringBuilder(value); | |
for (int i = 0; i < sb.length(); i++) { | |
int c = (int) sb.charAt(i); | |
if ((c >= 0xFF10 && c <= 0xFF19) || (c >= 0xFF21 && c <= 0xFF3A) || (c >= 0xFF41 && c <= 0xFF5A)) { | |
sb.setCharAt(i, (char)(c - 0xFEE0)); | |
} | |
} | |
return sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment