StringUtils 常用正則校驗

2021-09-24 16:44:09 字數 3443 閱讀 2506

精簡部落格內容,盡量已專業術語來分享。

努力做到對每一位認可自己的讀者負責。

幫助別人的同時更是豐富自己的良機。

一.常用校驗方法

/**

* 檢驗手機號

* @param phone

* @return

*/public

static boolean isphone

(string phone)

$");

.compile

("^((1[34578][0-9]))\\d$");

matcher matcher = regex.

matcher

(phone)

; boolean ismatched = matcher.

matches()

;return ismatched;

}/**

* 檢查是否全中文,返回true 表示是 反之為否

* @param realname

* @return

*/public

static boolean ischinese

(string realname)")

; matcher matcher = regex.

matcher

(realname)

; boolean ismatched = matcher.

matches()

;return ismatched;

}/**

* 檢查email是否是郵箱格式,返回true表示是,反之為否

* @param email

* @return

*/public

static boolean isemail

(string email)

/** * 檢查身份證的格式,返回true表示是,反之為否

* @param email

* @return

*/public

static boolean iscard

(string cardid)

((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d$");

//身份證正規表示式(18位)

pattern isidcard2=pattern.

compile

("^[1-9]\\d[1-9]\\d((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d([0-9]|x)$");

matcher matcher1= isidcard1.

matcher

(cardid)

; matcher matcher2= isidcard2.

matcher

(cardid)

; boolean ismatched = matcher1.

matches()

||matcher2.

matches()

;return ismatched;

}/**

* 判斷字串是否為整數

* @param str

* @return

*/public

static boolean isinteger

(string str)

pattern regex = pattern.

compile

("\\d*");

matcher matcher = regex.

matcher

(str)

; boolean ismatched = matcher.

matches()

;return ismatched;

}/**

* 判斷字串是否為數字

* @param str

* @return

*/public

static boolean isnumber

(string str)

pattern regex = pattern.

compile

("(-)?\\d*(.\\d*)?");

matcher matcher = regex.

matcher

(str)

; boolean ismatched = matcher.

matches()

;return ismatched;

}/**

* 判斷字串是否為純字母

* @param str

* @return

*/public

static boolean isenglish

(string str)

pattern regex = pattern.

compile

("[a-za-z]");

matcher matcher = regex.

matcher

(str)

; boolean ismatched = matcher.

matches()

;return ismatched;

}/**

* 根據身份證計算性別

* @param cardid

* @return

*/public

static int get***bycardid

(string cardid)

else

if (integer.parseint(***num) % 2 == 1) else */

int ***num =0;

if(cardid.

length()

==15

)else

if(***num %2==

1)else

}/**

* 根據身份證計算生日

* @param cardid

* @return

*/public

static string getbirthdaybycardid

(string cardid)

else

******dateformat sf1 =

new******dateformat

("yyyymmdd");

******dateformat sf2 =

new******dateformat

("yyyy-mm-dd");

string birthday =

null

;try

catch

(parseexception e)

return birthday;

}

StringUtils常用方法

isempty string str 是否為空,空格字元為false isnotempty string str 是否為非空,空格字元為true isblank string str 是否為空,空格字元為true isnotblank string str 是否為非空,空格字元為false trim...

StringUtils工具類常用方法

stringutils.strip 000000134 76539000 0 134 76539 stringutils.contains true stringutils.contains dfg true stringutils.replacechars asssdf s yyy ayyydf ...

StringUtils工具類常用方法

前言 工作中看到專案組裡的大牛寫 大量的用到了stringutils工具類來做字串的操作,便學習整理了一下,方便查閱。stringutils.strip 000000134 76539000 0 134 76539 stringutils.contains true stringutils.cont...