📄 stringutil.java
字号:
package com.common.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* String Utility Class This is used to encode passwords programmatically
*/
public class StringUtil {
// ~ Static fields/initializers
// =============================================
private final static Log log = LogFactory.getLog(StringUtil.class);
// ~ Methods
// ================================================================
/**
* Encode a string using algorithm specified in web.xml and return the
* resulting encrypted password. If exception, the plain credentials string
* is returned
*
* @param password
* Password or other credentials to use in authenticating this
* username
* @param algorithm
* Algorithm used to do the digest
*
* @return encypted password based on the algorithm.
*/
public static String encodePassword(String password, String algorithm) {
byte[] unencodedPassword = password.getBytes();
MessageDigest md = null;
try {
// first create an instance, given the provider
md = MessageDigest.getInstance(algorithm);
} catch (Exception e) {
log.error("Exception: " + e);
return password;
}
md.reset();
// call the update method one or more times
// (useful when you don't know the size of your data, eg. stream)
md.update(unencodedPassword);
// now calculate the hash
byte[] encodedPassword = md.digest();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < encodedPassword.length; i++) {
if ((encodedPassword[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
}
return buf.toString();
}
/**
* Encode a string using Base64 encoding. Used when storing passwords as
* cookies.
*
* This is weak encoding in that anyone can use the decodeString routine to
* reverse the encoding.
*
* @param str
* @return String
*/
public static String encodeString(String str) {
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
return encoder.encodeBuffer(str.getBytes()).trim();
}
/**
* Decode a string using Base64 encoding.
*
* @param str
* @return String
*/
public static String decodeString(String str) {
sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
try {
return new String(dec.decodeBuffer(str));
} catch (IOException io) {
throw new RuntimeException(io.getMessage(), io.getCause());
}
}
public static String formSearchString(String str) {
return "%" + str + "%";
}
public static String formDateString(String str) {
return "to_date('" + str + "','yyyy-mm-dd')";
}
public static boolean isEmpty(Object v) {
return null == v || "".equals(v.toString().trim());
}
public static long toLong(String str, long defaultVal) {
long v = 0;
try {
v = Long.parseLong(str);
} catch (Exception e) {
v = defaultVal;
}
return v;
}
public static int toInt(String str, int defaultVal) {
int v = 0;
try {
v = Integer.parseInt(str);
} catch (Exception e) {
v = defaultVal;
}
return v;
}
public static double toLong(String str, double defaultVal) {
double v = 0;
try {
v = Double.parseDouble(str);
} catch (Exception e) {
v = defaultVal;
}
return v;
}
public static String dealNull(String str) {
return str == null ? "" : str.trim();
}
/**
* turn a string array to flat form of single string
* @param array
* @param seperator
* @return
*/
public static String flatStringArray(String[] array, String seperator) {
StringBuffer buf = new StringBuffer();
if (null != array) {
for (int i = 0; i < array.length; i++) {
buf.append(array[i]).append(seperator);
}
if (buf.length() > 0) {
buf.delete(buf.length() - seperator.length(), buf.length());
}
}
return buf.toString();
}
/**
* 转码
* @param src
* @param characterSet
* @return
*/
public static String convertTOGBK(String src,String srcCharacterSet)
{
if(isEmpty(src)) return null;
try {
return new String(src.getBytes(srcCharacterSet),"GBK");
} catch (UnsupportedEncodingException e) {
return null;
}
}
public static String convertTOISO8859(String src,String srcCharacterSet)
{
if(isEmpty(src)) return null;
try {
return new String(src.getBytes(srcCharacterSet),"ISO8859_1");
} catch (UnsupportedEncodingException e) {
return null;
}
}
public static String filterString(String field,boolean isFilter)
{
String temp="";
if(isFilter)
{
temp=" case when (length("+field+") <= 10) then "+field+" when (length("+field+")>10) then substr("+field+", 0, 10) || '...'end ";
}
else
{
temp=field;
}
return temp;
}
/**
* 判断是不是正确电话号码
* @param phone
* @return
*/
public static boolean isRightTelePhone(String phone)
{
if(phone==null||phone.length()!=11) return false;
StringBuffer regex=new StringBuffer();
regex.append("^((15[0-9])|(13[0-9])|(0[1-2][0-9])|(0[3-8][0-9][0-9]))[0-9]{6,}");
regex.append("$");
Pattern p = Pattern.compile(regex.toString());
Matcher matcher=p.matcher(phone);
return matcher.matches();
}
// public static void main(String args[] ) {
// String array[] = {"hello","world"};
// System.out.println(flatStringArray(array,"##"));
// }
/**把学期学年转成开始时间
* 得到开始时间
*/
public static String getBeginTime(String year, String term) {
String beginTime="";
if(!isEmpty(year))
{
if(isEmpty(term)||"1".equals(term))
{
beginTime=year+"-02-01";
}
if("2".equals(term))
{
beginTime=year+"-09-01";
}
}
return beginTime;
}
/**把学期学年转成开始时间
* 得到结束时间
*/
public static String getEndTime(String year, String term) {
String endTime="";
if(!isEmpty(year))
{
if(isEmpty(term)||"1".equals(term))
{
endTime=year+"-08-01";
}
if("2".equals(term))
{
endTime=(Integer.parseInt(year)+1)+"-02-01";
}
}
return endTime;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -