/*
* 创建日期 2005-11-15
*/
package cn.com.skyon.sms.Utils;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.log4j.Logger;
public class Utils
{
private static Logger logger = Logger.getLogger(Utils.class);
public static final String[] EMPTYSTRS = new String[0];
/**
* 拆分字符串方法 copy from apache
*
* @author Apache
* @param str
* @param separator
* @return
*/
public static String[] Split(String str, String separator)
{
if (str == null)
{
return null;
}
int len = str.length();
if (len == 0)
{
return new String[0];
}
int separatorLength = separator.length();
ArrayList substrings = new ArrayList();
int numberOfSubstrings = 0;
int beg = 0;
int end = 0;
while (end < len)
{
end = str.indexOf(separator, beg);
if (end > -1)
{
if (end > beg)
{
numberOfSubstrings += 1;
if (numberOfSubstrings == -1)
{
end = len;
substrings.add(str.substring(beg));
}
else
{
substrings.add(str.substring(beg, end));
beg = end + separatorLength;
}
}
else
{
// substrings.add(null);
beg = end + separatorLength;
}
}
else
{
substrings.add(str.substring(beg));
end = len;
}
}
return (String[]) substrings.toArray(new String[substrings.size()]);
}
/**
* 代替String对象replace的方法 copy from apache
*
* @author Apache
* @param text
* @param repl
* @param with
* @return
*/
public static String replace(String text, String repl, String with, int max)
{
if (text == null || isEmpty(repl) || with == null || max == 0)
{
return text;
}
StringBuffer buf = new StringBuffer(text.length());
int start = 0, end = 0;
while ((end = text.indexOf(repl, start)) != -1)
{
buf.append(text.substring(start, end)).append(with);
start = end + repl.length();
if (--max == 0)
{
break;
}
}
buf.append(text.substring(start));
return buf.toString();
}
/**
* 计算字符串中包含子字符串的数量 copy from apache
*
* @param str
* @param sub
* @return
*/
public static int countMatches(String str, String sub)
{
if (isEmpty(str) || isEmpty(sub))
{
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1)
{
count++;
idx += sub.length();
}
return count;
}
/**
* 判断String是否为null 或为空字符串
*
* @param repl
* @return
*/
public static boolean isEmpty(String repl)
{
if(repl == null || repl.length() == 0)
return true;
for (int i = 0; i < repl.length(); i++)
{
if(repl.charAt(i) != ' ')
return false;
}
return true;
}
/**
* 对String数组中所有对象做trim
*
* @param strs
* @return
*/
public static String[] trimAll(String[] strs)
{
for (int i = 0; i < strs.length; i++)
{
String temp = strs[i];
if (temp != null)
{
strs[i] = strs[i].trim();
}
}
return strs;
}
/**
* ISO8859-1 转换成GB2312
*
* @param str
* @return
*/
public static String ansi2gbk(String str)
{
String result = str;
if (!isEmpty(result))
{
// try
// {
// result = new String(result.getBytes("ISO8859-1"), "GBK");
// }
// catch (UnsupportedEncodingException e)
// {
// logger.error("字符编码错误,impossible~!");
// }
return result;
}
return result;
}
/**
* ISO8859-1 转换成GB2312
*
* @param str
* @return
*/
public static String gbkToansi(String str)
{
String result = str;
if (!isEmpty(result))
{
try
{
result = new String(result.getBytes("GBK"), "ISO8859-1");
}
catch (UnsupportedEncodingException e)
{
logger.error("字符编码错误,impossible~!");
}
return result;
}
return result;
}
/**
* 判断一个数组是否是空数组
*
* @param array
* @return
*/
public static boolean isEmptyArray(Object[] array)
{
return (array == null || array.length == 0);
}
/**
* 判断手机号是否合法
*
* @return
*/
public static boolean isValidMobileNum(String mobileNumber)
{
// return mobileNumber != null && mobileNumber.length() == 11
// && mobileNumber.matches("^1\\d+");
return (!isEmpty(mobileNumber) && !"00000000000".equals(mobileNumber));
}
/**
* 将时间字符串例如16:10解析为int1610方便比较时间
*
* @param time
* @return
*/
public static int praseTime(String time)
{
if (isEmpty(time))
{
logger.warn("取得时间为空");
return 0;
}
if (time.length() != 5 || time.charAt(2)!=':')
{
logger.warn("时间格式不正确");
return 0;
}
String[] temp = Split(time , ":");
if (temp.length != 2) return 0;
String resultString = temp[0]+temp[1];
int result = 0;
try
{
result = new Integer(resultString).intValue();
}
catch (Exception e)
{
logger.error("解析时间失败");
return 0;
}
return result;
}
/**
* 转换字符串日期:20051214-->2005年12月14日
* @param date
* @return
*/
public static String constructDate(String date)
{
if (!isEmpty(date) && date.length()==8 && date.matches("\\d+"))
{
StringBuffer buffer = new StringBuffer();
buffer.append(date.substring(0,4)).append("年");
buffer.append(date.substring(4,6)).append("月");
buffer.append(date.substring(6)).append("日");
return buffer.toString();
}
return null;
}
public static String dealAccountNum(String account , int startPos , int endPos , char rep)
{
char[] charArray = account.toCharArray();
if(endPos > charArray.length)
endPos = charArray.length;
for (int i = startPos; i < endPos; i++)
{
charArray[i] = rep;
}
return new String(charArray);
}
public static String dealAccountNumDef(String account)
{
if(isEmpty(account))return null;
if(account.length() == 19)return dealAccountNum(account , 10 , 15 ,'*');
else if(account.length() == 16) return dealAccountNum(account , 4,12 , '*');
else if(account.length() == 20) return dealAccountNum(account , 5,11 , '*');
return account;
}
}