Created
August 5, 2014 03:55
-
-
Save esperia/55b647feae138d897258 to your computer and use it in GitHub Desktop.
半角を全角に変換
This file contains hidden or 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
package jp.co.kayo.android.test.util; | |
import android.test.AndroidTestCase; | |
public class TextToZenkakuTestCase extends AndroidTestCase { | |
private static final String CODES_SRC = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=[]\\;',./`~!@#$%^&*()_+{}|:\"<>?゙゚アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン"; | |
private static final String CODES_DEST = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=[]¥;',./`~!@#$%^&*()_+{}|:”<>?゛゜アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン"; | |
public static String toZenkaku(String src) { | |
StringBuilder result = new StringBuilder(); | |
char[] srcArray = src.toCharArray(); | |
char[] destArray = CODES_DEST.toCharArray(); | |
for (char chr : srcArray) { | |
int pos = CODES_SRC.indexOf(chr); | |
if (pos == -1) { | |
result.append(chr); | |
continue; | |
} | |
result.append(destArray[pos]); | |
} | |
return result.toString(); | |
} | |
public void testChars() { | |
String zenkaku = toZenkaku(CODES_SRC); | |
assertTrue(zenkaku.length() > 0); | |
for (int i = 0, il = zenkaku.length(); i < il; i++) { | |
assertEquals(CODES_DEST.charAt(i), zenkaku.charAt(i)); | |
} | |
String[][] testCases = new String[][] { | |
//@formatter:off | |
{ "a", "a" }, | |
{ "\\", "¥" }, | |
{ "1", "1" }, | |
{ "あ1い", "あ1い" }, | |
{ "ほげ!", "ほげ!" }, | |
{ "ア", "ア" }, | |
{ "ン", "ン" }, | |
{ "ポ", "ホ゜" }, | |
//@formatter:on | |
}; | |
for (String[] pattern : testCases) { | |
assertEquals(pattern[1], toZenkaku(pattern[0])); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
半角カタカナ→全角カタカナの対応はいってない!