import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtils {
public static final String PACKAGESPILTER = "|";
public static final String RECORDSPLITER = "^";
public static final String DELIMITER = "^";
public static final String BLANK = "?";
public static final String BETWEEN = ":";
public static final String BETWEEN_AND = ":";
public static final String OR = "~!";
public static final String CONTAIN = "*";
public static final int LENGTH_OR = 2;
public static final boolean CHANGECHARSET = false; // Unicode to GBK
private static NumberFormat format = new DecimalFormat("0.00");
public static final String DIRECTMODE = "Direct Mode!" + DELIMITER + DELIMITER;
/**
* 将byte值转换成字符串,当数值为0时返回"",否则返回数值字符串
*
* @param byteValue
* byte
* @return String 转换后的字符串
*/
public static String chgData(byte byteValue) {
String strReturn = null;
if (byteValue == 0) {
strReturn = "0";
} else {
strReturn = String.valueOf(byteValue);
}
return strReturn;
}
/**
* 转换数值字符串,当字符串变量的值为""或者为空时,将其转换为字符串"0"
*
* @param strValue
* String
* @return String 转换后的字符串
*/
public static String chgNumericStr(String strValue) {
if (strValue == null) {
return "0";
} else if ( "".equals(strValue.trim()) || strValue.length() == 0) {
return "0";
} else {
return strValue;
}
}
/**
* 转换布尔类型值
*
* @param strValue
* String
* @return String 布尔变量对应的中文描述
*/
public static String getBooleanDescribe(String strValue) {
String strReturn = strValue;
if (strValue.equals("Y") || strValue.equals("y")) {
strReturn = "是";
} else if (strValue.equals("N") || strValue.equals("n")) {
strReturn = "否";
}
return strReturn;
}
/**
* 如果一个字符串数字中小数点后全为零,则去掉小数点及零
*
* @param Value
* String
* @return String
*/
public static String getInt(String Value) {
if (Value == null) {
return null;
}
String result = "";
boolean mflag = true;
int m = 0;
m = Value.lastIndexOf(".");
if (m == -1) {
result = Value;
} else {
for (int i = m + 1; i <= Value.length() - 1; i++) {
if (Value.charAt(i) != '0') {
result = Value;
mflag = false;
break;
}
}
if (mflag) {
result = Value.substring(0, m);
}
}
return result;
}
/**
* 该函数得到c_Str中的第c_i个以c_Split分割的字符串
*
* @param c_Str
* 目标字符串
* @param c_i
* 位置
* @param c_Split
* 分割符
* @return 如果发生异常,则返回空
*/
public static String getStr(String c_Str, int c_i, String c_Split) {
String t_Str1 = "", t_Str2 = "", t_strOld = "";
int i = 0, i_Start = 0;
// int j_End = 0;
t_Str1 = c_Str;
t_Str2 = c_Split;
i = 0;
try {
while (i < c_i) {
i_Start = t_Str1.indexOf(t_Str2, 0);
if (i_Start >= 0) {
i += 1;
t_strOld = t_Str1;
t_Str1 = t_Str1.substring(i_Start + t_Str2.length(), t_Str1
.length());
} else {
if (i != c_i - 1) {
t_Str1 = "";
}
break;
}
}
if (i_Start >= 0) {
t_Str1 = t_strOld.substring(0, i_Start);
}
} catch (Exception ex) {
t_Str1 = "";
}
return t_Str1;
}
/**
* 将字符串补数,将sourString的<br>
* 后面用cChar补足cLen长度的字符串,如果字符串超长,则不做处理
* <b>Example: </b>
* RCh("Minim", "0", 10) returns "Minim00000"
*
* @param sourString
* 源字符串
* @param cChar
* 补数用的字符
* @param cLen
* 字符串的目标长度
* @return 字符串
*/
public static String RCh(String sourString, String cChar, int cLen) {
int tLen = sourString.length();
int i, iMax;
String tReturn = "";
if (tLen >= cLen) {
return sourString;
}
iMax = cLen - tLen;
for (i = 0; i < iMax; i++) {
tReturn += cChar;
}
tReturn = sourString.trim() + tReturn.trim();
return tReturn;
}
/**
* 修改RCH函数,可以在字符串后面补空格 将字符串补数,将sourString的<br>
* 后面用cChar补足cLen长度的字符串,如果字符串超长,则不做处理
* <b>Example: </b>
* RCh("Minim", "0", 10) returns "Minim00000"
* @param sourString
* 源字符串
* @param cChar
* 补数用的字符
* @param cLen
* 字符串的目标长度
* @return 字符串
*/
public static String RChSpace(String sourString, String cChar, int cLen) {
int tLen = sourString.length();
int i, iMax;
String tReturn = "";
if (tLen >= cLen) {
return sourString;
}
iMax = cLen - tLen;
for (i = 0; i < iMax; i++) {
tReturn += cChar;
}
tReturn = sourString.trim() + tReturn;
return tReturn;
}
/**
* 字符串比较函数(处理空值的情况,视null与空字符串相等) 比较字符串a,b
*
* @param a
* String
* @param b
* String
* @return boolean 如果相同返回true,否则返回fasle
*/
public static boolean compareString(String a, String b) {
// 这个地方是否需要修改,还需要进一步考虑,如果修改的话,仍需要保证null=""的情况
if (StringUtils.unicodeToGBK(StringUtils.cTrim(a)).equals(
StringUtils.unicodeToGBK(StringUtils.cTrim(b)))) {
return true;
} else {
return false;
}
}
/**
* 将字符串转换为GBK字符串
*
* @param strOriginal
* String 原串
* @return String 将原串由ISO8859_1(Unicode)编码转换为GBK编码
*/
public static String unicodeToGBK(String strOriginal) {
if (strOriginal != null) {
try {
// 如果在这里不作任何处理,全部直接返回的话,会是什么现象?
if (isGBKString(strOriginal)) {
// System.out.println("It's GBK: " + strOriginal);
return strOriginal;
} else {
// System.out.println("It's ISO8859_1: " + strOriginal);
return new String(strOriginal.getBytes("ISO8859_1"), "GBK");
}
} catch (Exception exception) {
System.out.println(exception.getMessage());
return strOriginal;
}
} else {
return "";
}
}
/**
* 判断是否是GBK编码
*
* @param tStr
* String
* @return boolean
*/
public static boolean isGBKString(String tStr) {
// int tlength = tStr.length();
// // Integer t = new Integer(0);
// int t1 = 0;
// for (int i = 0; i < tlength; i++)
// {
// t1 = Integer.parseInt(Integer.toOctalString(tStr.charAt(i)));
// if (t1 > 511)
// {
// return true;
// }
// }
// return false;
Pattern tPattern = Pattern.compile("[\\u4e00-\\u9fa5]");
Matcher tMatcher = tPattern.matcher(tStr);
return tMatcher.find();
}
/**
* 将输入的字符串进行转换,如果为空,则返回"",如果不空,则返回该字符串去掉前后空格
*
* @param tStr
* 输入字符串
* @return 如果为空,则返回"",如果不空,则返回该字符串去掉前后空格
*/
public static String cTrim(String tStr) {
String ttStr = "";
if (tStr == null) {
ttStr = "";
} else {
ttStr = tStr.trim();
}
return ttStr;
}
/**
* 复制文件,从fromFile复制到toFile中
*
* @param fromFile
* String
* @param toFile
* String
* @throws FileNotFoundException 没有找到指定的文件
* @throws IOException 读取或写入文件错误
*/
public static void copyFile(String fromFile, String toFile)
throws FileNotFoundException, IOException {
FileInputStream in = new FileInputStream(fromFile);
FileOutputStream out = new FileOutputStream(toFile);
byte b[] = new byte[1024];
int len;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.close();
in.close();
}
/**
* 将字符串按照指定的分隔字符进行拆分,返回从指定序号的分隔符到前一个分隔符之间的字符串
*
* @param strMain
* String 主字符串
* @param strDelimiters
* String 分隔符
* @param intSerialNo
* int 分隔符序号
* @return String 指定序号的分隔符到前一个分隔符之间的字符串,如果没有找到则返回"" 例如:值赋串类似于 值1|值2|值3|值4|
* 则intSerialNo=0 return "" intSerialNo=1 return "值1" intSerialNo=5
* return ""
*/
public static String decodeStr(String strMain, String strDelimiters,
int intSerialNo) {
int intIndex = 0; /* 分隔符出现在主字符串中的起始位置 */
int intCount = 0; /* 在扫描主字符串的过程中,第几次遇到分隔符字符串 */
String strReturn = ""; /* 作为返回值的字符串 */
if (strMain.length() < strDelimiters.length()) {
return ""; /* 若主字符串比分隔符串还要短的话,则返回空字符串 */
}
intIndex = strMain.indexOf(strDelimiters);
if (intIndex == -1) {
return ""; /* 若主字符串中不存在分隔符,则返回空字符串 */
}
while (intIndex != -1) /* 未找到分隔符时退出循环,并返回空字符串 */
{
strReturn = strMain.substring(0, intIndex);
intCount++;
if (intCount == intSerialNo) {
if (intIndex == 0) {
return "";
} else {
return strReturn.trim();
}
}
strMain = strMain.substring(intIndex + 1);
intIndex = strMain.indexOf(strDelimiters);
}
return "";
}
/**
* 字符串打包
*
* @param strInValue
* String
* @return String
*/
public static String encode(String strInValue) {
// String strOutValue = "";
StringBuffer tSBql = new StringBuffer();
char c;
for (int i = 0; i < strInValue.length(); i++) {
c = strInValue.charAt(i);
switch (c) {
case ':':
// hardcode 同Common.js中 NAMEVALUEDELIMITER //域名与域值的分隔符
tSBql.append(":");
// strOutValue += ":";
break;
case '|':
// hardcode 同Common.js中 FIELDDELIMITER //域之间的分隔符
tSBql.append("┃");
// strOutValue += "┃";
break;
case '\n':
tSBql.append("\\n");
// strOutValue += "\\n";
break;
case '\r':
tSBql.append("\\r");
// strOutValue += "\\r";
break;
case '\"':
tSBql.append("\\\"");
// strOutValue += "\\\"";
break;
case '\'':
tSBql.append("\\\'");
// strOutValue += "\\\'";
break;
case '\b':
tSBql.append("\\b");
// strOutValue += "\\b";
break;
case '\t':
tSBql.append("\\t");
// strOutValue += "\\t";
break;
case '\f':
tSBql.append("\\f");
// strOutValue += "\\f";
break;
case '\\':
tSBql.append("\\\\");
// strOutValue += "\\\\";
break;
case '<':
tSBql.append("\\<");
// strOutValue += "\\<";
break;
case '>':
tSBql.append("\\>");
// strOutValue += "\\>";
break;
default:
tSBql.append(c);
// strOutValue += c;
break;
}
}
return tSBql.toString();
}
/**
* 将字符串转换为Unicode字符串
*
* @param strOriginal
* String 原串
* @param realConvert
* boolean 是否确认转换
* @return String 将原串由GBK编码转换为ISO8859_1(Unicode)编码
*/
public static String GBKToUnicode(String strOriginal, boolean realConvert) {
if (!realConvert) {
return unicodeToGBK(strOriginal);
}
if (strOriginal != null) {
try {
if (isGBKString(strOriginal)) {
return new String(strOriginal.getBytes("GBK"), "ISO8859_1");
} else {
return strOriginal;
}
} catch (Exception exception) {
return strOriginal;
}
} else {
return null;
}
}
/**
* 获取子串在主串中出现第 n 次的位置
*
* @param strMain
* String 主字符串
* @param strSub
* String 子字符串
* @param intTimes
* int 出现次数
* @return int 位置值,如果子串在主串中没有出现指定次数,则返回-1
*/
public static int getPos(String strMain, String strSub, int intTimes) {
int intCounter = 0; // 循环记数
int intPosition = 0; // 位置记录
int intLength = strSub.length(); // 子串长度
if (intTimes <= 0) {
return -1;
}
while (intCounter < intTimes) {
intPosition = strMain.indexOf(strSub, intPosition);
if (intPosition == -1) {
return -1;
}
intCounter++;
intPosition += intLength;
}
return intPosition - intLength;
}
/**
* 获取从指定位置开始子串在主串中出现第 n 次的位置
*
* @param strMain
* String 主字符串
* @param strSub
* String 子字符串
* @param intStartIndex
* int 起始位置
* @param intTimes
* int 出现次数
* @return int 位置值,如果从起始位置起子串在主串中没有出现指定次数,则返回-1
*/
public static int getPos(String strMain, String strSub, int intStartIndex,
int intTimes) {
if (strMain.length() - 1 < intStartIndex) {
return -1;
}
int intPosition = getPos(strMain.substring(intStartIndex), strSub,
intTimes);
if (intPosition != -1) {
intPosition += intStartIndex;
}
return intPosition;
}
/**
* 以指定内容生成给定长度的字符串,不足以指定字符按指定方式补齐,超长截去
*
* @param strValue
* String 指定内容
* @param intLength
* int 字符串长度
* @param appendchar
* char 指定字符
* @param LRApp
* char 指定方式 L:左补齐 R:右补齐
* @return String
*/
public static String getStringWith(String strValue, int intLength,
char appendchar, char LRApp) {
int strLen = strValue.length();
StringBuffer strReturn = new StringBuffer();
if (strLen > intLength) {
strReturn.append(strValue.substring(0, intLength));
} else {
if (strLen == 0) {
strReturn.append(appendchar);
} else {
if (LRApp == 'R') {
strReturn.append(strValue);// 右补齐
}
}
for (int i = strLen; i < intLength; i++) {
strReturn.append(appendchar);
}
if (strLen > 0) {
if (LRApp == 'L') {
strReturn.append(strValue);// 左补齐
}
}
}
return strReturn.toString();
}
/**
* 字符串替换函数
*
* @param strMain
* String 原串
* @param strFind
* String 查找字符串
* @param strReplaceWith
* String 替换字符串
* @return String 替换后的字符串,如果原串为空或者为"",则返回""
*/
public static String replace(String strMain, String strFind,
String strReplaceWith) {
// String strReturn = "";
StringBuffer tSBql = new StringBuffer();
int intStartIndex = 0;
int intEndIndex = 0;
if (strMain == null || strMain.equals("")) {
return "";
}
while ((intEndIndex = strMain.indexOf(strFind, intStartIndex)) > -1) {
// strReturn = strReturn +
// strMain.substring(intStartIndex, intEndIndex) +
// strReplaceWith;
tSBql.append(strMain.substring(intStartIndex, intEndIndex));
tSBql.append(strReplaceWith);
intStartIndex = intEndIndex + strFind.length();
}
// strReturn += strMain.substring(intStartIndex, strMain.length());
tSBql.append(strMain.substring(intStartIndex, strMain.length()));
return tSBql.toString();
}
/**
* 字符串替换函数
*
* @param strMain
* String 原串
* @param strFind
* String 查找字符串
* @param strReplaceWith
* String 替换字符串,在替换时不区分大小写
* @return String 替换后的字符串,如果原串为空或者为"",则返回""
*/
public static String replaceEx(String strMain, String strFind,
String strReplaceWith) {
// String strReturn = "";
StringBuffer tSBql = new StringBuffer();
String tStrMain = strMain.toLowerCase();
String tStrFind = strFind.toLowerCase();
int intStartIndex = 0;
int intEndIndex = 0;
if (strMain == null || strMain.equals("")) {
return "";
}
while ((intEndIndex = tStrMain.indexOf(tStrFind, intStartIndex)) > -1) {
// strReturn = strReturn +
// strMain.substring(intStartIndex, intEndIndex) +
// strReplaceWith;
tSBql.append(strMain.substring(intStartIndex, intEndIndex));
tSBql.append(strReplaceWith);
intStartIndex = intEndIndex + strFind.length();
}
// strReturn += strMain.substring(intStartIndex, strMain.length());
tSBql.append(strMain.substring(intStartIndex, strMain.length()));
return tSBql.toString();
}
/**
* 生成给定长度的字符串
*
* @param intLength
* int 字符串长度
* @return String
*/
public static String space(int intLength) {
StringBuffer strReturn = new StringBuffer();
for (int i = 0; i < intLength; i++) {
strReturn.append(" ");
}
return strReturn.toString();
}
/**
* 以指定内容生成给定长度的字符串,不足以空格补齐,超长截去
*
* @param strValue
* String 指定内容
* @param intLength
* int 字符串长度
* @return String
*/
public static String space(String strValue, int intLength) {
int strLen = strValue.length();
StringBuffer strReturn = new StringBuffer();
if (strLen > intLength) {
strReturn.append(strValue.substring(0, intLength));
} else {
if (strLen == 0) {
strReturn.append(" ");
} else {
strReturn.append(strValue);
}
for (int i = strLen; i < intLength; i++) {
strReturn.append(" ");
}
}
return strReturn.toString();
}
/**
* 字符串转换成HTML格式
*
* @param strInValue
* String 传入字符串
* @return String 转换后返回
*/
public static String toHTMLFormat(String strInValue) {
// String strOutValue = "";
StringBuffer tSBql = new StringBuffer();
char c;
for (int i = 0; i < strInValue.length(); i++) {
c = strInValue.charAt(i);
switch (c) {
case '<':
tSBql.append("<");
// strOutValue += "<";
break;
case '>':
tSBql.append(">");
// strOutValue += ">";
break;
case '\n':
tSBql.append("<br>");
// strOutValue += "<br>";
break;
case '\r':
break;
case ' ':
tSBql.append(" ");
// strOutValue += " ";
break;
default:
tSBql.append(c);
// strOutValue += c;
break;
}
}
return tSBql.toString();
}
/**
* 根据输入标记为间隔符号,拆分字符串
*
* @param strMain
* String
* @param strDelimiters
* String 失败返回NULL
* @return String[]
*/
public static String[] split(String strMain, String strDelimiters) {
int i;
int intIndex = 0; // 记录分隔符位置,以取出子串
Vector<String> vResult = new Vector<String>(); // 存储子串的数组
String strSub = ""; // 存放子串的中间变量
strMain = strMain.trim();
// 若主字符串比分隔符串还要短的话,则返回空字符串
if (strMain.length() <= strDelimiters.length()) {
System.out.println("分隔符串长度大于等于主字符串长度,不能进行拆分!");
return null;
}
// 取出第一个分隔符在主串中的位置
intIndex = strMain.indexOf(strDelimiters);
// 在主串中找不到分隔符
if (intIndex == -1) {
String[] arrResult = { strMain };
return arrResult;
}
// 分割主串到数组中
while (intIndex != -1) {
strSub = strMain.substring(0, intIndex);
if (intIndex != 0) {
vResult.add(strSub);
} else {
// break;
vResult.add("");
}
strMain = strMain.substring(intIndex + strDelimiters.length())
.trim();
intIndex = strMain.indexOf(strDelimiters);
}
// 如果最末不是分隔符,取最后的字符串
// if (!strMain.equals("") && strMain != null)
if (!strMain.equals("")) {
vResult.add(strMain);
}
String[] arrResult = new String[vResult.size()];
for (i = 0; i < vResult.size(); i++) {
arrResult[i] = (String) vResult.get(i);
}
return arrResult;
}
/**
* 转换JavaScript解析不了的特殊字符
*
* @param s
* String
* @return String
*/
public static String changForJavaScript(String s) {
char[] arr = s.toCharArray();
s = "";
for (int i = 0; i < arr.length; i++) {
if (arr[i] == '"' || arr[i] == '\'' || arr[i] == '\n') {
s += "\\";
}
s += arr[i];
}
return s;
}
/**
* 将二维数组进行数据打包
*
* @param arr
* String[][] 存储数据的二维数组
* @return String 按照编码规则将数组转换后得到的字符串
*/
public static String encode(String arr[][]) {
System.out.println("使用StringUtils下的encode方法打包");
// String strReturn = "";
StringBuffer tSBql = new StringBuffer();
int rowcount = arr.length; // 取得数组的行数
int colcount = arr[0].length; // 取得数组的列数
int eleCount = rowcount * colcount;
if (eleCount != 0) {
tSBql.append("0");
tSBql.append(PACKAGESPILTER);
tSBql.append(String.valueOf(rowcount));
tSBql.append(RECORDSPLITER);
// strReturn = "0" + SysConst.PACKAGESPILTER +
// String.valueOf(rowcount) +
// SysConst.RECORDSPLITER;
for (int i = 0; i < rowcount; i++) {
for (int j = 0; j < colcount; j++) {
if (j != colcount - 1) {
tSBql.append(arr[i][j]);
tSBql.append(PACKAGESPILTER);
// strReturn = strReturn + arr[i][j] +
// SysConst.PACKAGESPILTER;
}
}
if (i != rowcount - 1) {
tSBql.append(RECORDSPLITER);
// strReturn += SysConst.RECORDSPLITER;
}
}
}
return tSBql.toString();
}
/**
* 将数字格式化为金额形式 0.00
*
* @param value 待格式化的值
* @return String 格式化后的字符串
*/
public static String formatDec(double value) {
try {
return format.format(value);
} catch (Exception ex) {
return String.valueOf(value);
}
}
/**
* 将字符串转换为Unicode字符串
*
* @param strOriginal
* String 原串
* @return String 将原串由GBK编码转换为ISO8859_1(Unicode)编码
*/
public static String GBKToUnicode(String strOriginal) {
if (CHANGECHARSET) {
if (strOriginal != null) {
try {
if (isGBKString(strOriginal)) {
return new String(strOriginal.getBytes("GBK"),
"ISO8859_1");
} else {
return strOriginal;
}
} catch (Exception exception) {
return strOriginal;
}
} else {
return null;
}
} else {
// 不可以随意修改这里的返回,否则导致系统存入的数据有问题
// return unicodeToGBK(strOriginal);
// System.out.println("Don't unicodeToGBK ......");
if (strOriginal == null) {
return "";
} else {
return strOriginal;
}
}
}
/**
* 获取日期值中的年份
*
* @param strDate
* String 日期(年/月/日)
* @return String 年
*/
public static String getYear(String strDate) {
int intPosition = 0;
String strReturn = "";
int intYear = 0;
if ((strDate != null) && (strDate.trim().length() > 0)) {
intPosition = StringUtils.getPos(strDate, DateUtils.DATEDELIMITER, 1);
if (intPosition > 0) {
strReturn = strDate.substring(0, intPosition);
intYear = new Integer(strReturn).intValue();
if ((intYear <= 0)) {
strReturn = "";
} else {
strReturn = "" + intYear;
}
if ((intYear < 10) && (!strReturn.equals(""))) {
strReturn = "0" + strReturn;
}
}
}
return strReturn;
}
/**
* 转换字符串(主要是SQL语句的条件串)
*
* @param strMessage
* String 待转换的字符串 (形如:<I>字段名^操作符^字段值^</I>) 新格式为:{[(]字段名 操作符 字段值
* [)]连接符号}版本格式串^
* @return String 返回字符串(SQL语句中的 WHERE 条件子句,但不包括 'where'关键字)
*/
public static String makeCondition(String strMessage) {
String strSegment = "";
String strField = "";
String strOperator = "";
String strValue = "";
String strRemain = "";
String strReturn = "1=1"; // 恒真条件
int intPosition = 0;
if (strMessage.indexOf(DELIMITER) < 0) {
return strMessage;
}
strRemain = strMessage;
if (!strRemain.endsWith(DIRECTMODE + DELIMITER)) {
do {
intPosition = getPos(strRemain, DELIMITER, 3);
if (intPosition < 0) // 分解完毕
{
return strReturn;
}
strSegment = strRemain.substring(0, intPosition + 1).trim();
strRemain = strRemain.substring(intPosition + 1);
if (strSegment.length() < 1) // 分段结束
{
break;
}
strField = decodeStr(strSegment, DELIMITER, 1);
strOperator = decodeStr(strSegment, DELIMITER, 2);
strValue = decodeStr(strSegment, DELIMITER, 3);
if (strValue.equals(BLANK)) {
strValue = " ";
}
strReturn = strReturn + " AND " + "(";
// 判断操作符
if (strOperator.equals(BETWEEN)) {
intPosition = strValue.indexOf(BETWEEN_AND);
strReturn = strReturn + strField + " BETWEEN '"
+ strValue.substring(0, intPosition).trim() + "'"
+ " AND '"
+ strValue.substring(intPosition + 1).trim() + "' ";
} else if (strOperator.equals(CONTAIN)) {
strReturn = strReturn + strField + " matches '" + strValue
+ "*' ";
} else {
strSegment = "";
while (true) {
intPosition = strValue.indexOf(OR);
if (intPosition < 0) {
if (strSegment.equals("")) {
strReturn = strReturn + strField + " "
+ strOperator + " '" + strValue + "' ";
} else {
strReturn = strReturn + strSegment + " OR "
+ strField + " " + strOperator + " '"
+ strValue.trim() + "' ";
}
break;
}
if (!strSegment.equals("")) {
strSegment += " OR ";
}
strSegment = strSegment + strField + " " + strOperator
+ " '"
+ strValue.substring(0, intPosition).trim()
+ "' ";
strValue = strValue.substring(intPosition + LENGTH_OR);
}
}
strReturn += ") ";
} while (true);
} else {
strRemain = strRemain.substring(0, strRemain.length()
- DIRECTMODE.length() - DELIMITER.length());
if (strRemain.trim().equals("")) {
strRemain = "1=1";
}
strReturn = strRemain;
}
return strReturn;
}
/**
* 使用指定类中的decode()方法解包给定字符串
*
* @param strMessage
* String 字符串
* @param intCount
* int 解包次数
* @param cl
* Class 包含decode()方法的类,并且此类中含有FIELDNUM属性
* @return Vector 将每个解包数据生成对应的类实例,并将这些实例作为返回Vector的元素
* @throws Exception
* 如果查找decode方法出错、或者查找FIELDNUM字段出错、或者解包出错,方法抛出异常
*/
public static Vector stringToVector(String strMessage, int intCount,
Class cl) throws Exception {
int intFieldNum = 0;
Object object = null;
Vector vec = new Vector();
int intPosition = 0;
Class[] parameters = { String.class };
Method method = null;
Field field = null;
String[] therecord = { new String() };
try {
object = cl.newInstance();
method = cl.getMethod("decode", parameters);
field = cl.getField("FIELDNUM");
intFieldNum = field.getInt(object);
for (int i = 0; i < intCount; i++) {
object = cl.newInstance();
intPosition = StringUtils.getPos(strMessage,
PACKAGESPILTER, intFieldNum);
if (intPosition == strMessage.length() - 1) {
therecord[0] = strMessage;
method.invoke(object, therecord);
vec.addElement(object);
break;
} else {
therecord[0] = strMessage.substring(0, intPosition + 1);
method.invoke(object, therecord);
vec.addElement(object);
strMessage = strMessage.substring(intPosition + 1);
}
}
} catch (Exception exception) {
throw exception;
} finally {
}
return vec;
}
/**
* 该函数将字符串中的特殊字符转化为其他字符
* 在jsp文件中使用application.getRealPath("config//Conversion.config")得到绝对路径
*
* @param strIn
* 源字符串
* @param pathname
* 配置文件的绝对路径文件名
* @return 返回修改后的字符串
*/
/*public static String Conversion(String strIn, String pathname)
{
int i;
String strOut = "";
SSRS tSSRS;
try
{
ConfigInfo.SetConfigPath(pathname);
tSSRS = ConfigInfo.GetValuebyCon();
for (i = 0; i < tSSRS.MaxRow; i++)
{
strOut = replace(strIn, tSSRS.GetText(i + 1, 1), tSSRS.GetText(i + 1, 2));
if (i != tSSRS.MaxRow - 1)
{
strIn = strOut;
}
}
}
catch (Exception ex)
{
strOut = "";
}*/
}
StringUtils
最新推荐文章于 2023-12-29 16:43:09 发布