Skip to content

Instantly share code, notes, and snippets.

@amano41
Created May 7, 2014 04:08
Show Gist options
  • Save amano41/8b8191e462db28c8cb16 to your computer and use it in GitHub Desktop.
Save amano41/8b8191e462db28c8cb16 to your computer and use it in GitHub Desktop.
全角⇔半角の変換
/**
* 半角 ⇒ 全角
*/
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 &lt; 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