package com.xjj.framework.utils;
/**
* @author xjj
*/
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
public class StringUtils {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(StringUtils.class);
/**
* 将字符串转化为中文编码gb2312,默认输入的格式为iso-8859-1
*
* @param strInput
* 输入字符串
* @return
*/
public static String convertToChinese(String strInput) {
return convertToChinese(strInput, "iso-8859-1");
}
/**
* 将字符串转化为中文编码gb2312
*
* @param strInput
* 输入文本
* @param srcEncode
* 输入的格式
* @return
*/
public static String convertToChinese(String strInput, String srcEncode) {
String str = "";
try {
str = new String(strInput.getBytes(srcEncode), "gb2312");
} catch (Exception e) {
logger.error(e.getMessage());
return strInput;
}
return str;
}
/**
* 将列表中的文本前后加入指定字符
*
* @param list
* 字符串列表
* @param appStr
* 添加的指定字符
* @return
*/
public static List<String> appendAll(List<String> list, String appStr) {
List<String> tmpList = new ArrayList<String>();
;
for (String str : list) {
str = appStr + str + appStr;
tmpList.add(str);
}
return tmpList;
}
/**
* 将列表中的文本前面加入指定字符
*
* @param list
* 字符串列表
* @param appStr
* 添加的指定字符
* @return
*/
public static List<String> appendFore(List<String> list, String appStr) {
List<String> tmpList = new ArrayList<String>();
;
for (String str : list) {
str = appStr + str;
tmpList.add(str);
}
return tmpList;
}
/**
* 将列表中的文本后面加入指定字符
*
* @param list
* 字符串列表
* @param appStr
* 添加的指定字符
* @return
*/
public static List<String> appendBack(List<String> list, String appStr) {
List<String> tmpList = new ArrayList<String>();
;
for (String str : list) {
str = str + appStr;
tmpList.add(str);
}
return tmpList;
}
/**
* 将列表中的字符串合并
*
* @param list
* 列表
* @param delim
* 分隔符
* @return
*/
public static String join(List<String> list, String delim) {
if (list == null || list.size() < 1) {
return null;
}
if (delim == null) {
delim = "";
}
StringBuffer buf = new StringBuffer();
for (String str : list) {
buf.append(str);
buf.append(delim);
}
return buf.toString().substring(0, buf.toString().lastIndexOf(delim));
}
/**
* 将列表中的字符串合并
*
* @param list
* 列表
* @return
*/
public static String join(Object... list) {
if (list == null || list.length == 0) {
return "";
}
StringBuilder builder = new StringBuilder();
for (Object c : list) {
builder.append(c);
}
return builder.toString();
}
/**
* 将字符窜拆成数组
*
* @param str
* 带拆分的字符串
* @param delim
* 分隔符
* @return
*/
public static String[] splitToArray(String str, String delim) {
if (delim == null || delim.equals(""))
delim = ",";
String[] ret = new String[] {};
if (str == null || str.equals("")) {
return ret;
}
if (str.indexOf(delim) < 0) {
return new String[] { str.trim() };
}
Vector<String> splitList = new Vector<String>();
for (String s : str.split(delim)) {
String trimmed = s.trim();
if (trimmed.length() > 0)
splitList.add(trimmed);
}
return splitList.toArray(ret);
}
/**
* 将字符窜拆成List列表
*
* @param str
* 带拆分的字符串
* @param delim
* 分隔符
* @return
*/
public static List<String> splitToList(String str, String delim) {
if (delim == null || delim.equals(""))
delim = ",";
List<String> splitList = new ArrayList<String>();
for (String s : str.split(delim)) {
String trimmed = s.trim();
if (trimmed.length() > 0)
splitList.add(trimmed);
}
return splitList;
}
/**
* 将字符窜拆成Set
*
* @param str
* 带拆分的字符串
* @param delim
* 分隔符
* @return
*/
public static Set<String> splitToSet(String str, String delim) {
if (delim == null || delim.equals(""))
delim = ",";
Set<String> splitSet = new HashSet<String>();
;
for (String s : str.split(delim)) {
String trimmed = s.trim();
if (trimmed.length() > 0)
splitSet.add(trimmed);
}
return splitSet;
}
/**
* 截断过长的以空格分割的单词
*
* @param str
* 被截断的字符串
* @param maxLength
* 最大长度
* @return
*/
public static String createBreaks(String str, int maxLength) {
char chars[] = str.toCharArray();
int len = chars.length;
StringBuffer buf = new StringBuffer(len);
int count = 0;
int cur = 0;
for (int i = 0; i < len; i++) {
if (Character.isWhitespace(chars[i])) {
count = 0;
}
if (count >= maxLength) {
count = 0;
buf.append(chars, cur, i - cur).append(" ");
cur = i;
}
count++;
}
buf.append(chars, cur, len - cur);
return buf.toString();
}
/**
* 格式化SQL语句标志
*
* @param str
* @return
*/
public static String escapeSQLTags(String str) {
if (str == null || str.length() == 0) {
return str;
}
StringBuffer buf = new StringBuffer();
char ch = ' ';
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
if (ch == '\\') {
buf.append("\\\\");
} else if (ch == '\'') {
buf.append("\'\'");
} else {
buf.append(ch);
}
}
return buf.toString();
}
/**
* 格式化html文本,将html文本进行编码
*
* @param str
* @return
*/
public static String escapeHTMLTags(String str) {
if (str == null || str.length() == 0) {
return str;
}
StringBuffer buf = new StringBuffer();
char ch = ' ';
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
if (ch == '<') {
buf.append("<");
} else if (ch == '>') {
buf.append(">");
} else if (ch == '&') {
buf.append("&");
} else if (ch == '"') {
buf.append(""");
} else if (ch == ' ') {
buf.append(" ");
} else {
buf.append(ch);
}
}
return buf.toString();
}
/**
* 反格式化html文本,将html中的编码转化为对应格式
*
* @param str
* @return
*/
public static String toHTMLTags(String str) {
if (str == null || str.length() == 0) {
return str;
}
return str.replaceAll("<", "<").replaceAll(">", ">")
.replaceAll("&", "&").replaceAll(""", "\"")
.replaceAll(" ", " ");
}
/**
* 将换行转换为html标签
*
* @param str
* @return
*/
public static String convertNewlines(String str) {
str = replace(str, "\r\n", "<P>");
str = replace(str, "\n", "<BR>");
return str;
}
/**
* 替换字符串中的字符
*
* @param str
* 待替换的字符串
* @param oldString
* 旧字符
* @param newString
* 新字符
* @return
*/
public static String replace(String str, String oldString, String newString) {
if (str == null) {
return null;
}
int i = str.lastIndexOf(oldString);
if (i < 0) {
return str;
}
StringBuffer mainSb = new StringBuffer(str);
while (i >= 0) {
mainSb.replace(i, i + oldString.length(), newString);
i = str.lastIndexOf(oldString, i - 1);
}
return mainSb.toString();
}
/**
* 检�