package com.sjw.tool;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
/**
*
*
* 标题:项目研发
*
* 模块:公共部分
*
* 公司: yyang
*
* 作者:xb,2009-3-5
*
* 描述:
*
* 说明:
*
*/
public class StrUtil {
/**
* 数字格式对象
*/
private static NumberFormat formater = DecimalFormat.getInstance();
// 默认除法运算精度
private static final int DEF_DIV_SCALE = 10;
/*
* 在数字前补零,并返回相应的字符 weishu 补零后的字符长度 num 要补零的数字 例 String a=addZero(5,321); 得到 00321
*/
public static String addZero(int weishu, int num) {
/* int num=new Integer(num).intValue(); */
int len = Integer.toString(num).length();
if (len >= weishu) {
return Integer.toString(num);
}
int i = 0;
int j = weishu - len;
String BH = "";
while (i < j) {
BH = "0" + BH;
i = i + 1;
}
BH = BH + Integer.toString(num);
return BH;
}
/**
* 对输入字符要求是字符或数字类型的有效性判断。 常用在口令上。
*/
public static boolean checkIDpass(String aim) throws IOException {
String num = "0123456789";
String lowcase = "abcdefghijklmnopqrstuvwxyz";
String upcase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String single;
int i;
int length = aim.length();
int errnum;
int errlow;
int errup;
for (i = 0; i < length; i++) {
single = aim.substring(i, i + 1);
errnum = num.indexOf(single);
errlow = lowcase.indexOf(single);
errup = upcase.indexOf(single);
if (errnum == -1 && errlow == -1 && errup == -1) {
return false;
}
}
return true;
}
// 得到数字相除的余数。
// down 除数 up被除数
public static int getMod(float up, float down) throws IOException {
int result = 0;
float dev = up / down;
result = (int) dev;
// if(dev>result){result+=1;}
result = dev > result ? result + 1 : result;
return result;
}
/**
* 字符串内容是否是一个时间
*
* @param str
* @return
*/
@SuppressWarnings("static-access")
public static boolean isTime(String str) {
try {
java.sql.Time time = null;
time = time.valueOf(str);
return true;
} catch (Exception e) {
return false;
}
}
/**
* 字符串内容是否是一个日期
*
* @param str
* @return
*/
@SuppressWarnings("static-access")
public static boolean isDate(String str) {
try {
if ((str == null) || (str.equals(""))) {
return false;
} else {
str = str.trim();
}
java.sql.Date date = null;
date = date.valueOf(str);
if (!str.equals(date.toString())) {
return false;
}
return true;
} catch (Exception e) {
return false;
}
}
/**
* 字符串内容是否是一个整数
*/
public static boolean isInteger(String str) throws NumberFormatException {
try {
if ((str == null) || (str.equals(""))) {
return false;
} else {
str = str.trim();
}
int thisInt = (new Integer(str)).intValue();
if (thisInt < 0) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
/**
* 字符串内容是否是一个数字
*
* @param str
* @return
* @throws NumberFormatException
*/
public static boolean isNumeric(String str) throws NumberFormatException {
try {
if ((str == null) || (str.equals(""))) {
return false;
} else {
str = str.trim();
}
@SuppressWarnings("unused")
float thisfloat = (new Float(str)).floatValue();
return true;
} catch (Exception e) {
return false;
}
}
/**
* 根据关键字将字符串转为数组
*
* @param Str
* @param keyStr
* @return
*/
@SuppressWarnings("unchecked")
public static String[] toStringArray(String Str, String keyStr) {
ArrayList sarr = new ArrayList();
int startIndex = Str.indexOf(keyStr);
while (startIndex >= 0) { // 存在出现位置
String startString = Str.substring(0, startIndex); // 在这之前的字符
if (!startString.equals("")) {
sarr.add(startString);
}
Str = Str.substring(startIndex + keyStr.length(), Str.length()); // 在这之后的字符
startIndex = Str.indexOf(keyStr);
}
if (Str.length() > 0) {
sarr.add(Str);
}
int i = sarr.size();
String[] s = new String[i];
for (int j = 0; j < i; j++) {
s[j] = (String) sarr.get(j);
}
return s;
}
/**
* 字符替换方法 str 需要处理的字符串 suo 需要替换掉的字符串 des需要替换为的字符串
*/
public static String replaceString(String Str, String Sou, String Des) {
int startIndex;
int endIndex;
String startString;
String endString;
StringBuffer returnString = new StringBuffer();
if (Str == null) {
Str = "";
returnString.append(Str);
} else {
while (true) {
startIndex = Str.indexOf(Sou);
if (startIndex >= 0) { // 存在出现位置
endIndex = startIndex + Sou.length(); // 结束位置
startString = Str.substring(0, startIndex); // 在这之前的字符
if (startString == null) {
startString = "";
}
endString = Str.substring(endIndex, Str.length()); // 在这之后的字符
if (endString == null) {
endString = "";
}
returnString.append(startString + Des); // 返回字符串
Str = endString; // 原字符串改为新串
} else {
returnString.append(Str);
break;
}
}
}
return returnString.toString();
}
public static String replaceStringS(String str, String oldSubStr, String newSubStr) {
if (str == null) {
return str;
}
StringBuffer sb = new StringBuffer();
int i = 0;
int j = 0;
int len = oldSubStr.length();
while ((i = str.indexOf(oldSubStr, j)) != -1) {
sb.append(str.substring(j, i));
sb.append(newSubStr);
j = i + len;
}
sb.append(str.substring(j, str.length()));
return sb.toString();
}
/**
* 本方法把一些需要屏蔽的字符串改为对应的中文字符。这样不会对页面显示造成错误
*/
public static String replaceHtmlString(String str) {
if (str == null) {
str = "";
} else {
str = str.trim();
str = replaceString(str, "<", "〈");
str = replaceString(str, ">", "〉");
str = replaceString(str, "'", "‘");
str = replaceString(str, "\"", "“");
}
return str;
}
/**
* 格式化数字字符
*
* @param psParm
* @param psFormat
* @param psLocals
* @return
*/
public static String formatNumber(String psParm, String psFormat, String psLocals) {
psParm = String.valueOf(strTodouble(psParm));
if (psFormat.equals("") & psLocals.equals("")) {
return psParm;
}
NumberFormat form;
if (psLocals.equals("PERCENT")) {
form = NumberFormat.getPercentInstance(Locale.CHINA);
} else {
if (psLocals.equals("CURRENCY")) {
form = NumberFormat.getCurrencyInstance(Locale.CHINA);
} else {
form = NumberFormat.getInstance(Locale.CHINA);
((DecimalFormat) form).applyPattern(psFormat);
}
}
return form.format(new Double(psParm));
}
/**
* 字符串转整形数字
*
* @param s
* @return
* @exception IllegalArgumentException
* if this method is invoked
*/
public static int stringToInt(String s) {
try {
return Integer.valueOf(s).intValue();
} catch (Exception e) {
return -1;
}
}
/**
* 字符串转double
*
* @param s
* @return
*/
public static double strTodouble(String s) {
if (s == null || s.trim().length() <= 0) {
return 0.0D;
}
s = s.trim();
String s1 = "";
String s2 = "";
int ie1 = s.indexOf('e');
if (ie1 < 0) {
ie1 = s.indexOf('E');
}
if (ie1 < 0) {
s1 = s;
} else {
s1 = s.substring(0, ie1).trim();
s2 = s.substring(ie1 + 1).trim();
}