public class StringUtils {

    /**
* 如果str为null,返回“”,否则返回str
* @param str
* @return
*/
public static String isNull(String str) {
if (str == null) {
return "";
}
return str.trim();
} public static String isNull(Object o) {
if (o == null) {
return "";
}
String str="";
if(o instanceof String){
str=(String)o;
}else{
str=o.toString();
}
return str;
} /**
* 检验手机号
* @param phone
* @return
*/
public static boolean isPhone(String phone){
phone = isNull(phone);
Pattern regex = Pattern
// .compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
.compile("^((1[34578]{1}[0-9]))\\d{8}$");
Matcher matcher = regex.matcher(phone);
boolean isMatched = matcher.matches();
return isMatched;
} /**
* 检查是否全中文,返回true 表示是 反之为否
* @param realname
* @return
*/
public static boolean isChinese(String realname){
realname = isNull(realname);
Pattern regex = Pattern.compile("[\\u4e00-\\u9fa5]{2,25}");
Matcher matcher = regex.matcher(realname);
boolean isMatched = matcher.matches();
return isMatched;
} /**
* 检查email是否是邮箱格式,返回true表示是,反之为否
* @param email
* @return
*/
public static boolean isEmail(String email) {
email = isNull(email);
Pattern regex = Pattern
.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
Matcher matcher = regex.matcher(email);
boolean isMatched = matcher.matches();
return isMatched;
} /**
* 检查身份证的格式,返回true表示是,反之为否
* @param email
* @return
*/
public static boolean isCard(String cardId) {
cardId = isNull(cardId);
//身份证正则表达式(15位)
Pattern isIDCard1=Pattern.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$");
//身份证正则表达式(18位)
Pattern isIDCard2=Pattern.compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([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) {
if (isEmpty(str)) {
return false;
}
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) {
if (isEmpty(str)) {
return false;
}
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) {
if (isEmpty(str)) {
return false;
}
Pattern regex = Pattern.compile("[a-zA-Z]{1,}");
Matcher matcher = regex.matcher(str);
boolean isMatched = matcher.matches();
return isMatched;
} /**
* 判断字符串是否为空
* @param str
* @return
*/
public static boolean isEmpty(String str) {
if (str == null || "".equals(str)) {
return true;
}
return false;
} /**
* 首字母大写
* @param s
* @return
*/
public static String firstCharUpperCase(String s) {
StringBuffer sb = new StringBuffer(s.substring(0, 1).toUpperCase());
sb.append(s.substring(1, s.length()));
return sb.toString();
} public static String hideChar(String str,int len){
if(str==null) return null;
char[] chars=str.toCharArray();
for(int i=1;i<chars.length-1;i++){
if(i<len){
chars[i]='*';
}
}
str=new String(chars);
return str;
} public static String hideFirstChar(String str,int len){
if(str==null) return null;
char[] chars=str.toCharArray();
if(str.length()<=len){
for(int i=0;i<1;i++){
chars[i]='*';
}
}else{
for(int i=0;i<len;i++){
chars[i]='*';
}
}
str=new String(chars);
return str;
} public static String hideLastChar(String str,int len){
if(str==null) return null;
char[] chars=str.toCharArray();
if(str.length()<=len){
for(int i=0;i<chars.length;i++){
chars[i]='*';
}
}else{
for(int i=chars.length-1;i>chars.length-len-1;i--){
chars[i]='*';
}
}
str=new String(chars);
return str;
} public static String hideNumber(String str){
if(str==null) return null;
char[] chars=str.toCharArray();
if(str.length()<=7){
for(int i=0;i<chars.length;i++){
chars[i]='*';
}
}else{
for(int i=3;i<chars.length-4;i++){
chars[i]='*';
}
}
str=new String(chars);
return str;
} /**
*
* @return
*/
public static String format(String str,int len){
if(str==null) return "-";
if(str.length()<=len){
int pushlen=len-str.length();
StringBuffer sb=new StringBuffer();
for(int i=0;i<pushlen;i++){
sb.append("0");
}
sb.append(str);
str=sb.toString();
}else{
String newStr=str.substring(0, len);
str=newStr;
}
return str;
} public static String contact(Object[] args){
StringBuffer sb=new StringBuffer();
for(int i=0;i<args.length;i++){
sb.append(args[i]);
if(i<args.length-1){
sb.append(",");
}
}
return sb.toString();
} /**
* 是否包含在以“,”隔开字符串内
* @param s
* @param type
* @return
*/
public static boolean isInSplit(String s,String type){
if(isNull(s).equals("")){
return false;
}
List<String> list=Arrays.asList(s.split(","));
if(list.contains(type)){
return true;
}
return false;
} public static boolean isBlank(String str){
return StringUtils.isNull(str).equals("");
} public synchronized static String generateTradeNO(long userid,String type){
String s;
s = type + userid + getFullTimeStr();
return s;
} public static String getFullTimeStr(){
String s=DateUtils.dateStr3(Calendar.getInstance().getTime());
return s;
} public static String array2Str(Object[] arr){
StringBuffer s=new StringBuffer();
for(int i=0;i<arr.length;i++){
s.append(arr[i]);
if(i<arr.length-1){
s.append(",");
}
}
return s.toString();
} public static String array2Str(int[] arr){
StringBuffer s=new StringBuffer();
for(int i=0;i<arr.length;i++){
s.append(arr[i]);
if(i<arr.length-1){
s.append(",");
}
}
return s.toString();
} /**
* 指定起始位置字符串隐藏
* @param str
* @param index1
* @param index2
* @return
*/
public static String hideStr(String str, int index1, int index2) {
if (str == null)
return null;
String str1 = str.substring(index1, index2);
String str2 = str.substring(index2);
String str3 = "";
if (index1 > 0) {
str1 = str.substring(0, index1);
str2 = str.substring(index1, index2);
str3 = str.substring(index2);
}
char[] chars = str2.toCharArray();
for (int i = 0; i < chars.length; i++) {
chars[i] = '*';
}
str2 = new String(chars);
String str4 = str1 + str2 + str3;
return str4;
} // 四舍五入保留两位小数点
public static String SetNumberFractionDigits(String str) {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
return nf.format(Float.valueOf(str));
}
public static String getMonday(String the_rq){
int n=getXC_days(the_rq);
//System.out.println("n="+n);
n=n*-1;
return Q_N_Day(n,the_rq);
} //获取输入日期的星期天日期 public static String getSunday(String the_rq){
int n=getXC_days(the_rq);
//System.out.println("n="+n);
n=(6-(n*-1))*-1;
return Q_N_Day(n,the_rq);
} // 获得输入日期与本周一相差的天数
public static int getXC_days(String rq){
SimpleDateFormat formatYMD=new SimpleDateFormat("yyyy-MM-dd");//formatYMD表示的是yyyy-MM-dd格式
SimpleDateFormat formatD=new SimpleDateFormat("E");//"E"表示"day in week"
Date d=null;
String weekDay="";
int xcrq=0;
try{
d=formatYMD.parse(rq);//将String 转换为符合格式的日期
weekDay=formatD.format(d);
if(weekDay.equals("星期一")){
xcrq=0;
}else{
if(weekDay.equals("星期二")){
xcrq=-1;
}else{
if(weekDay.equals("星期三")){
xcrq=-2;
}else{
if(weekDay.equals("星期四")){
xcrq=-3;
}else{
if(weekDay.equals("星期五")){
xcrq=-4;
}else{
if(weekDay.equals("星期六")){
xcrq=-5;
}else{
if(weekDay.equals("星期日")){
xcrq=-6;
} } } }
}
}
}
}catch (Exception e){
e.printStackTrace();
}
return xcrq;
} public static String Q_N_Day(int N,String d1){//
String []d2=d1.split("-");
int year=Integer.parseInt(d2[0]);
int month=Integer.parseInt(d2[1]);
int day=Integer.parseInt(d2[2]);
if(day-N<=0){
if(month==1){
year=year-1;
month=12;
day = day + 31-N;
}else{
month=month-1;
if (month == 2) {
if (year % 4 == 0) {
day = day + 29-N;
} else {
day = day + 28-N;
}
}else{
if(month==4||month==6||month==9||month==11){
day=day+30-N;
}else{
day=day+31-N;
}
}
}
}else{
///////////////////////////////////////////////////////////////////////////////////
if(month==12){
if((day-N)>31){
year=year+1;
month=1;
day=(day-N)-31;
}else{
day=day-N;
}
}else{
if (month == 2) {
if (year % 4 == 0) {
if((day-N)>29){
month++;
day=(day-N)-29;
}else{
day=day-N;
}
} else {
if((day-N)>28){
month++;
day=(day-N)-28;
}else{
day=day-N;
}
}
}else{
if(month==4||month==6||month==9||month==11){
if((day-N)>30){
month++;
day=(day-N)-30;
}else{
day=day-N;
}
}else{
if((day-N)>31){
month++;
day=(day-N)-31;
}else{
day=day-N;
}
}
}
} //day=day-N;
}
String str=String.valueOf(year);
if(month<10){
str=str+"-0"+String.valueOf(month);
}else{
str=str+"-"+String.valueOf(month);
}
if(day<10){
str=str+"-0"+String.valueOf(day);
}else{
str=str+"-"+String.valueOf(day);
} return str;
} /*public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
String mondayString=StringUtils.getMonday(df.format(new Date()))+ " 00:00:00";
String sumdayString=StringUtils.getSunday(df.format(new Date()))+ " 23:59:59";
String monday=DateUtils.getTime(mondayString)+"";
String sumday=DateUtils.getTime(sumdayString)+"";
//System.out.println(monday);
//System.out.println(sumday); }*/ public static String fillTemplet(String templet, String phstr, String[] paras){
StringBuffer templetSB = new StringBuffer(templet);
int i = 0;
while(templetSB.indexOf(phstr) >= 0 && i < paras.length){
templetSB.replace(templetSB.indexOf(phstr), templetSB.indexOf(phstr)+phstr.length(), paras[i]);
i++;
}
return templetSB.toString();
}
//V1.6.6.1 RDPROJECT-226 liukun 2013-09-26 start
/*public static String fillTemplet(String template){
//V1.6.6.1 RDPROJECT-331 liukun 2013-10-12 start
//模板中的'是非法字符,会导致无法提交,所以页面上用`代替
template = template.replace('`', '\'');
//V1.6.6.1 RDPROJECT-331 liukun 2013-10-12 end Map<String,Object> data=Global.getTransfer();
try {
return FreemarkerUtil.renderTemplate(template, data);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}*/
//V1.6.6.1 RDPROJECT-226 liukun 2013-09-26 end //V1.6.5.3 RDPROJECT-142 liukun 2013-09-11 start
public static int[] strarr2intarr(String[] strarr){
int[] result = new int[strarr.length];
for(int i=0;i<strarr.length;i++)
{
result[i] = Integer.parseInt(strarr[i]);
}
return result;
} /**
* 大写字母转成“_”+小写
* @param str
* @return
*/
public static String toUnderline(String str){
char[] charArr=str.toCharArray();
StringBuffer sb=new StringBuffer();
sb.append(charArr[0]);
for(int i=1;i<charArr.length;i++){
if(charArr[i]>='A'&&charArr[i]<='Z'){
sb.append('_').append(charArr[i]);
}else{
sb.append(charArr[i]);
}
}
return sb.toString().toLowerCase();
} /**
* 根据身份证计算性别
* @param cardId
* @return
*/
public static int getSexByCardid(String cardId) {
/*String sexNum = "";
if (cardId.length() == 15) {
sexNum = cardId.substring(13, 14);
} else {
sexNum = cardId.substring(16, 17);
} if (Integer.parseInt(sexNum) % 2 == 1) {
return 1;
} else {
return 0;
}*/
int sexNum = 0;
if (cardId.length() == 15) {
sexNum = cardId.charAt(13);
} else {
sexNum = cardId.charAt(16);
}
if (sexNum % 2 == 1) {
return 1;
} else {
return 0;
}
} /**
* 根据身份证计算生日
* @param cardId
* @return
*/
public static String getBirthdayByCardid(String cardId) {
String birth = null;
if (cardId.length() == 15) {
birth = cardId.substring(6, 12);
} else {
birth = cardId.substring(6, 14);
}
SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
String birthday = null;
try {
birthday = sf2.format(sf1.parse(birth));
} catch (ParseException e) {
e.printStackTrace();
}
return birthday;
} public static String getNullStr(Object obj) {
if(obj==null) {
return "";
}
return obj.toString();
} public static String getFileSuffixName(String fileName){
String suffix = null;
if (fileName != null) {
int last = fileName.lastIndexOf('.');
suffix = fileName.substring(last);
}
return suffix;
}
/**
* 格式化数字
* @param num
* @return
*/
public static String getFormatNumber(String num){
int number=Integer.parseInt(num);
if(Integer.parseInt(num) >= 100000) {
BigDecimal accountB = new BigDecimal(number);
return accountB.divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_DOWN).stripTrailingZeros().toPlainString()+"万元";
} else {
return number+"元";
}
} }
  

StringUtils 正则校验的更多相关文章

  1. RegExp正则校验之Java及R测试

    前言: 正则表达式(英语:Regular Expression)原属于计算机科学的一个概念.正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串.在很多文本编辑器里边,正则表达式通常被用 ...

  2. java_method_正则校验

    /** * * @Title: validadeRegExp * @Descrption : TODO (正则校验) * @param regExp * @param obj * @return * ...

  3. 正则校验:微信号,qq号,邮箱

    java判断微信号.手机.名字的正则表达 - willgos - 博客园https://www.cnblogs.com/solossl/p/5813106.html 微信号正则校验,qq正则,邮箱正则 ...

  4. 最全,可直接用的一些正则校验,判断邮箱,手机号码,车牌号,身份证号,网址,账号,密码,ip,去掉html格式,工商税号等。

    一些正则校验,判断邮箱,手机号码,车牌号,身份证号,网址,账号,密码,ip,去掉html格式,工商税号等. // 判断邮箱 isValid = [text isValidEmail]; // 判断手机 ...

  5. 校验总结:校验是否是中英文等等(1.正则校验 2.hibernate volidator)

    1.正则校验 import java.util.regex.Matcher;import java.util.regex.Pattern; public class Validation { //-- ...

  6. jQuery正则校验

    jQuery正则校验 银行卡号 //验证银行卡号,bankno为银行卡号function luhnCheck(){ var bankno = $.trim($("#bankNoInp&quo ...

  7. js前台传数组,java后台接收转list,前后台用正则校验

    前台,传参数时,将数组对象转换成json串,后台java收到后用 JSONArray.fromObject 转成集合. 前台js:var params = {"FileNameList&qu ...

  8. Django的form组件——正则校验

    from django.contrib import admin from django.urls import path from app01 import views urlpatterns = ...

  9. js电话号码正则校验--座机和手机号

    1.最新的电话号码段: 移动:134(1349除外)135 136 137 138 139 147 150 151 152 157 158 159 182 183 184 187 188 联通: 13 ...

随机推荐

  1. 洛谷P2444 病毒【AC自动机】

    题目描述 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的.现在委员会已经找出了所有的病毒代码段,试问,是否 ...

  2. python threading.thread

    Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法:另一种是创建一个threading.Thread对 ...

  3. linux 分卷压缩和合并

      压缩: 可以用任何方式压缩,如tar -czf 分卷: split [OPTION]... [INPUT [PREFIX]]    -b 代表分卷大小, 后面可以加单位,如G,M,K.   如果不 ...

  4. This module embeds Lua, via LuaJIT 2.0/2.1, into Nginx and by leveraging Nginx's subrequests, allows the integration of the powerful Lua threads (Lua coroutines) into the Nginx event model.

    openresty/lua-nginx-module: Embed the Power of Lua into NGINX HTTP servers https://github.com/openre ...

  5. python之sys.stdout、sys.stdin以及设置打印到日志文件等

    转自:https://www.cnblogs.com/BigFishFly/p/6622784.html python之sys.stdout.sys.stdin 转自:http://www.cnblo ...

  6. sql server 驱动程序在 \Device\RaidPort0 上检测到控制器错误。

    sql server 驱动程序在 \Device\RaidPort0 上检测到控制器错误. 错误情况,如下图: 原因分析:硬盘故障 解决办法:进行迁移

  7. 【工具】代码生成器-python脚本

    我觉得造轮子这件事情,是谁都可以做的.只不过做得好或者不好而已,用心了做得就要优雅一点. 之前用过java的代码生成器,什么pojodobodbo都能生成,于是我也来自己造一个轮子. 造轮子的事情是没 ...

  8. django高级之点赞、文章评论及上传文件

    目录: 点赞 文章评论 上传文件 保留页面条件 一.点赞 1.所用技术: django model F查询 js应用:$(function () {}); 为文件加载完成执行ready() 方法.等同 ...

  9. STL学习笔记--序列式容器

    1.vector vector是一个线性顺序结构.相当于数组,但其大小可以不预先指定,并且自动扩展.故可以将vector看作动态数组. 在创建一个vector后,它会自动在内存中分配一块连续的内存空间 ...

  10. ubuntu update-alternatives

    update-alternatives是ubuntu系统中专门维护系统命令链接符的工具,通过它可以很方便的设置系统默认使用哪个命令.哪个软件版本,比如,我们在系统中同时安装了open jdk和sun ...