字符串工具类

字符串常用工具类

public class Util {

//判断对象是否为空

public static boolean isNullOrEmpty(Object obj) {
if (obj == null) {
return true;
} else if (obj instanceof Object[]) {
return ((Object[]) obj).length == 0;
} else if (obj instanceof List) {
return ((List) obj).isEmpty();
} else if (obj instanceof Map) {
return ((Map) obj).isEmpty();
} else {
if (obj instanceof Date) {
return obj.equals(new Date(0));
} else if (obj instanceof String) {
return isNullOrEmpty((String) obj);
}
}
return false;
}


//判断字符串是否为空

public static boolean isNullOrEmpty(String inStr) {
return (inStr == null || inStr.trim().length() == 0);
}


// 将字符串格式化为日期

public static java.util.Date getString2Date(String date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
java.util.Date obj = null;
try {
obj = sdf.parse(date);
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}

//将日期格式化为字符串

public static String getDate2String(java.util.Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
//左边补内容
 
public static String appendString(String paramString, int paramInt,
String str) {
if (paramString == null)
paramString = "";
int i = paramInt - paramString.length();
if (i <= 0)
return paramString.substring(0, paramInt);
StringBuffer StrBuffer = new StringBuffer(str);
for (int j = 0; j < i - 1; ++j)
StrBuffer.append(str);


return StrBuffer.append(paramString).toString();
}

// 截取字符串

public static String subStr(String message, int maxLength, String charset) {
try {
byte[] bytes = message.getBytes(charset);
if (bytes.length > maxLength) {
byte[] bs = new byte[maxLength];
System.arraycopy(bytes, 0, bs, 0, maxLength);
String msg = new String(bs, charset);
return msg;
} else {
return message;
}
} catch (Exception e) {
return null;
}
}


public static String subStr(String message, int maxLength) {
return subStr(message, maxLength, "UTF-8");
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

powerfuler

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值