package cn.com;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class MyStringFilter {
static public final String LINEBREAK1, LINEBREAK2, PAGEBREAK;
static {
char[] creturn1 = { 0x0a };
LINEBREAK1 = new String(creturn1);
char[] creturn = { 0x0d, 0x0a };
LINEBREAK2 = new String(creturn);
char[] preturn = { 0x0c, 0x1e };
PAGEBREAK = new String(preturn);
}
static public String makeToOneLine(String originalString) {
return replace(replace(originalString, LINEBREAK2, " "), LINEBREAK1, " ");
}
static public String replace(String originalString, String replacedString, String replaceWith) {
if (originalString == null)
return null;
int loc = originalString.indexOf(replacedString);
if (loc < 0)
return originalString;
int limit = originalString.length();
StringBuffer sb = new StringBuffer((int) (limit * 1.5));
int loc0 = 0;
int jump = replacedString.length();
while (loc >= 0) {
sb.append(originalString.substring(loc0, loc));
sb.append(replaceWith);
loc0 = loc + jump;
if (loc0 >= limit)
loc = -2;
else
loc = originalString.indexOf(replacedString, loc0);
}
if (loc == -1)
sb.append(originalString.substring(loc0));
return sb.toString();
}
static public String replaceFirst(String originalString, String replacedString, String replaceWith) {
if (originalString == null)
return null;
int loc = originalString.indexOf(replacedString);
if (loc < 0)
return originalString;
int jump = replacedString.length();
StringBuffer sb = new StringBuffer();
sb.append(originalString.substring(0, loc));
sb.append(replaceWith);
sb.append(originalString.substring(loc + jump));
return sb.toString();
}
static public String filterJavaScriptParam(String original) {
String ret = replace(original, "\\", "\\\\");
ret = replace(ret, "'", "\\'");
ret = replace(ret, "\"", "\\\"");
return ret;
}
static public String filterInLineSQL(String original) {
return replace(original, "'", "''");
}
static public String filterExcelExport(String original) {
return replace(original, "<", "LESS THAN"); // excel is confused on some
// html tag such as <HEAD
}
static public String filterXmlStr(String original) {
String ret = replace(original, "&", "&");
ret = replace(ret, "<", "<");
ret = replace(ret, ">", ">");
ret = replace(ret, "'", "'");
return ret;
}
static public String filterTextStr(String original) {
String ret = replace(original, "&", "&");
ret = replace(ret, "<", "<");
ret = replace(ret, ">", ">");
ret = replace(ret, "'", "'");
return ret;
}
static public String filter5010Str(String original) {
String ret = replace(original, "*", "");
ret = replace(ret, ":", "");
ret = replace(ret, "^", "");
ret = replace(ret, "~", "");
return ret;
}
/**
* This method also removes the ')' from the string, besides all the changes
* make by filterInLineSQL.
*/
static public String filterInLineSQLNoBracket(String original) {
String changed1 = original.replace(')', ' ');
return filterInLineSQL(changed1);
}
static public String filterUrl(String original) {
if (MyStringFilter.isEmpty(original))
return "";
else
return java.net.URLEncoder.encode(original);
// return java.net.URLEncoder.encode(original, "UTF-8");
}
public static final String filterDbOrFileName(String s) {
// keep only letter and digit and "." and "_" and "$"
if (s == null)
return "";
else {
StringBuffer sb = new StringBuffer(s.length());
for (int i = 0; i < s.length(); i++) {
char aChar = s.charAt(i);
if ((aChar >= 'a' && aChar <= 'z') || (aChar >= 'A' && aChar <= 'Z') || (aChar >= '0' && aChar <= '9')
|| aChar == '.' || aChar == '_' || aChar == '$')
sb.append(aChar);
}
return sb.toString();
}
}
static public boolean isEmpty(String s) {
if (s == null || s.length() == 0)
return true;
else
return false;
}
static public boolean isEmpty(String[] Arr) {
if (Arr != null) {
for (int i = 0; i < Arr.length; i++) {
if (!isEmpty(Arr[i]))
return false;
}
}
return true;
}
static public boolean isEmptyOrZero(String s) {
if (isEmpty(s))
return true;
try {
double d = Double.parseDouble(s);
if (d == 0.0)
return true;
else
return false;
} catch (NumberFormatException e) {
return false;
}
}
static public final double NEGLIGIBLE_MONEY = 0.009;
static public final double NEGLIGIBLE_NUMBER = 0.0000001;
static public boolean isEmptyOrZeroMoney(String s) {
return isEmptyOrZero(s, NEGLIGIBLE_MONEY);
}
static public boolean isEmptyOrZeroUnit(String s) {
return isEmptyOrZero(s, NEGLIGIBLE_NUMBER);
}
static public boolean isEmptyOrZero(String s, double threshHold) {
if (isEmpty(s))
return true;
try {
double d = Double.parseDouble(s);
if (d == 0.0 || Math.abs(d) < NEGLIGIBLE_MONEY)
return true;
else
return false;
} catch (NumberFormatException e) {
return false;
}
}
static public String noNull(String s) {
if (s == null)
return "";
else
return s;
}
static public final boolean isStrictYYYYMMDD(String input) {
if (input == null || input.length() != 8)
return false;
try {
SimpleDateFormat YYYYMMDD = new SimpleDateFormat("yyyyMMdd");
YYYYMMDD.setLenient(false);
YYYYMMDD.parse(input);
// last char must be numeric -- otherwise "2001123," would be
// accepted
char lastChar = input.charAt(input.length() - 1);
if (lastChar >= '0' && lastChar <= '9')
return true;
else
return false;
} catch (ParseException e) {
return false;
}
}
static public String noNullTrim(String s) {
if (s == null)
return "";
else
return s.trim();
}
static public String noNullTrimUpper(String s) {
if (s == null)
return "";
else
return s.trim().toUpperCase();
}
static public String[] noNullStringArr(String[] Arr, int size) {
if (Arr == null) {
String[] ret = new String[size];
Arrays.fill(ret, "");
return ret;
} else
return Arr;
}
static public String leftTrim(String val) {
if (isEmpty(val))
return "";
else {
int i = 0;
for (; i < val.length(); i++) {
char aChar = val.charAt(i);
if (aChar > 0x20)
break;
}
if (i == 0)
return val;
else
return val.substring(i);
}
}
static public String rightTrim(String val) {
if (isEmpty(val))
return "";
else {
int i = val.length() - 1;
for (; i > 0; i--) {
char aChar = val.charAt(i);
if (aChar > 0x20)
break;
}
if (i == val.length() - 1)
return val;
else
return val.substring(0, i + 1);
}
}
/**
* Convert a boolean to int representation true -> 1; false ->0
*/
static public int booleanToInt(boolean b) {
if (b)
return 1;
else
return 0;
}
/**
* Convert a integer to c style boolean representation, ie. 0->false;
* anythingelse -> true
*/
static public boolean intToBoolean(int i) {
if (i == 0)
return false;
else
return true;
}
/**
* @returns The parsed double value of the input quantity if it is parsable;
* 0.0 if it is null or not a number.
*/
static public double getDoubleNum(String quantity) {
if (MyStringFilter.isEmpty(quantity))
return 0.0;
else {
try {
return Double.parseDouble(quantity);
} catch (NumberFormatException e) {
return 0.0;
}
}
}
/**
* @returns The parsed int value of the input quantity if it is parsable; 0
* if it is null or not a number.
*/
static public int getIntNum(S
- 1
- 2
前往页