Skip to content

Instantly share code, notes, and snippets.

@mabushiisign
Created November 9, 2012 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mabushiisign/4048324 to your computer and use it in GitHub Desktop.
Save mabushiisign/4048324 to your computer and use it in GitHub Desktop.
Javaの文字種チェック処理。記号、英数、ひらがな、カタカナ、JIS第1水準と第2水準を許可。「//# + ” \ < > ; ‘ ,」は不許可。
/**
* 文字種チェック.
* @param target チェック対象文字列
* @return 文字種不正 false
*/
public static boolean checkChar(final String target){
//文字種NGフラグ
boolean ngflg;
//Stringをchar配列に変換
char[] charArray = target.toCharArray();
//一文字ずつループ処理
for (int i = 0; i < charArray.length; i++) {
//エラーフラグをtrueに設定
ngflg = true;
char c = charArray[i];
int targetChar = getSJISByte(c);
if((0x8140 <= targetChar) && (targetChar <= 0x8396)) {
// 1区~5区:OK
if (targetChar != 0x8148) {
//?でない
ngflg = false;
}
}
if((0x889F <= targetChar) && (targetChar <= 0xEAA4)) {
// 16区~84区:OK
ngflg = false;
}
if ((0x20 <= targetChar) && (targetChar <= 0x7E)) {
// ASCII
if (targetChar != 0x23 && targetChar != 0x2B && targetChar != 0x22
&& targetChar != 0x5C && targetChar != 0x3C && targetChar != 0x3E
&& targetChar != 0x3B && targetChar != 0x27 && targetChar != 0x2C) {
//# + " \ < > ; ' ,でない
ngflg = false;
}
}
if (ngflg) {
return false;
}
}
return true;
}
/**
* Shift-JISでバイトに変換する.
* @param c 変換対象文字
* @return 変換後文字
*/
private static int getSJISByte(final char c) {
try {
//Windows-31Jでbyteに変換
byte[] bArray = String.valueOf(c).getBytes("Windows-31J");
int targetChar;
if (bArray.length == 1) {
//1バイト文字
targetChar = bArray[0] & 0xFF;
} else {
//2バイト文字
targetChar = ((bArray[0] & 0xFF) << 8) | (bArray[1] & 0xFF);
}
return targetChar;
} catch (Exception e) {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment