これ以外にも必要なメソッドはたくさんあるけど、まずはこれくらい。
また、これと同じ処理を別の書き方もできるよ。
次回は、別の書き方で作成します。
import java.util.regex.Pattern;
public class StringUtil {
/**
* strがnullか""(空文字)の場合に、trueを返す。
*
* @param str 入力値
* @return boolean true : 入力無し / false : 入力有り
*/
public static boolean isEmpty(String str) {
if (str == null || "".equals(str)) {
return true;
}
return false;
}
/**
* alphabetがアルファベットの場合に、trueを返す。
*
* @param alphabet アルファベットの文字列
* @return boolean true : アルファベット / false : アルファベット以外
*/
public static boolean isAlphabet(String alphabet) {
try {
return !(Pattern.compile("[^a-zA-Z]")).matcher(alphabet).matches();
} catch (Exception e) {
return false;
}
}
/**
* characterが英数字の場合に、trueを返す。
*
* @param character 英数字の文字列
* @return boolean true : 英数字 / false : 英数字以外
*/
public static boolean isNumAlphabet(String character) {
try {
return !(Pattern.compile("[^a-zA-Z0-9]")).matcher(character).matches();
} catch (Exception e) {
return false;
}
}
/**
* str1がstr2より大きい場合に、trueを返す。
*
* @param str1 英数字の文字列1
* @param str2 英数字の文字列2
* @return boolean true : 文字列1が大きい / false : 文字列1が大きくない
*/
public static boolean charGreaterThan(String str1, String str2) {
try {
return str1.compareTo(str2) > 0;
} catch (Exception e) {
return false;
}
}
/**
* str1がstr2より小さい場合に、trueを返す。
*
* @param str1 英数字の文字列1
* @param str2 英数字の文字列2
* @return boolean true : 文字列1が小さい / false : 文字列1が小さくない
*/
public static boolean charSmallerThan(String str1, String str2) {
try {
return str1.compareTo(str2) < 0;
} catch (Exception e) {
return false;
}
}
/**
* str1がstr2以上の場合に、trueを返す。
*
* @param str1 英数字の文字列1
* @param str2 英数字の文字列2
* @return boolean true : 文字列1が以上 / false : 文字列1が以上ではない
*/
public static boolean charGreaterThanEqual(String str1, String str2) {
try {
return !StringUtil.charSmallerThan(str1, str2);
} catch (Exception e) {
return false;
}
}
/**
* str1がstr2以下の場合に、trueを返す。
*
* @param str1 英数字の文字列1
* @param str2 英数字の文字列2
* @return boolean true : 文字列1が以下 / false : 文字列1が以下ではない
*/
public static boolean charSmallerThanEqual(String str1, String str2) {
try {
return !StringUtil.charGreaterThan(str1, str2);
} catch (Exception e) {
return false;
}
}
/**
* zenkakuが全角文字の場合に、trueを返す。
*
* @param zenkaku 全角の文字列
* @return boolean true : 全角 / false : 全角以外
*/
public static boolean isZenkaku(String zenkaku) {
try {
return !(Pattern.compile("[ -~]")).matcher(zenkaku).matches();
} catch (Exception e) {
return false;
}
}
/**
* strがstrArrayに存在している場合に、indexを返す。
*
* @param str 文字列
* @return int 0,1,2...n : strArrayに存在する / -1 : strArrayに存在しない
*/
public static int exist(String str, String[] strArray) {
int count = 0;
if (strArray == null) {
return -1;
}
count = strArray.length;
for (int i = 0; i < count; i++) {
if (str.startsWith(strArray[i])) {
return i;
}
}
return -1;
}
}
0 件のコメント:
コメントを投稿