package com.css.common.util;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.commons.lang.StringUtils;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
/**
* 字符串工具集合
*
* @version 1.0
*
*/
public class StringUtil extends org.apache.commons.lang.StringUtils {
public static String Array2String(String[] ids) {
if (ids == null) {
return "";
}
StringBuffer s = new StringBuffer();
for (int i = 0; i < ids.length; i++) {
s.append(ids[i]);
s.append(",");
}
s.setLength(s.length() - 1);
return s.toString();
}
public static List String2List(String ids) {
List list = new ArrayList();
if (ids == null) {
return list;
}
String[] idArray = ids.split(",");
for (int i = 0; i < idArray.length; i++) {
list.add(idArray[i]);
}
return list;
}
public static List<Integer> Array2List(String[] ids) {
if (ids == null) {
return new ArrayList<Integer>();
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < ids.length; i++) {
list.add(Integer.parseInt(ids[i]));
}
return list;
}
public static List<Long> Array2LongList(String[] ids) {
if (ids == null) {
return new ArrayList<Long>();
}
List<Long> list = new ArrayList<Long>();
for (int i = 0; i < ids.length; i++) {
list.add(Long.parseLong(ids[i]));
}
return list;
}
public static String formatStr(int length, String str) {
if (FormatUtil.isNotNull(str)) {
if (str.length() < length) {
length = str.length();
}
String title = str.substring(0, length);
return title;
}
return str;
}
public static String formatStr(int length, String str, String suffix) {
if (FormatUtil.isNotNull(str)) {
String title = formatStr(length, str);
if (str.length() > length) {
title = title + suffix;
}
return title;
}
return str;
}
/**
* 过滤字符串中所有的空格
* @return
*/
public static String trim(String str) {
String temp = "";
for (int i = 0; i < str.length(); i++) {
temp = (new StringBuilder()).append(temp).append(str.substring(i, i + 1).trim()).toString();
}
return temp;
}
/**
* Compares two strings, character by character, and returns the first
* position where the two strings differ from one another.
*
* @param s1
* The first string to compare
* @param s2
* The second string to compare
* @return The first position where the two strings differ.
*/
public static final int stringDifference(String s1, String s2) {
int len1 = s1.length();
int len2 = s2.length();
int len = len1 < len2 ? len1 : len2;
for (int i = 0; i < len; i++) {
if (s1.charAt(i) != s2.charAt(i)) {
return i;
}
}
return len;
}
private final static NodeFilter nodeFilter = new NodeFilter() {
public boolean accept(Node node) {
return true;
}
};
/**
* retrive the extend name of the given filename
*
* @param fn
* @return
*/
public static String getFileExtend(String fn) {
if (isEmpty(fn))
return null;
int idx = fn.lastIndexOf('.') + 1;
if (idx == 0 || idx >= fn.length())
return null;
return fn.substring(idx);
}
/**
* 将字符串用ch分割并放入队列
*
* @param tags
* @param ch
* @return
*/
public static List stringToList(String tags, String ch) {
if (tags == null)
return null;
ArrayList tagList = new ArrayList();
StringTokenizer st = new StringTokenizer(tags, ch);
while (st.hasMoreElements()) {
tagList.add(st.nextToken());
}
return tagList;
}
/**
* 将字符串用空格分割并放入队列
*
* @param tags
* @return
*/
public static List stringToList(String tags) {
if (tags == null)
return null;
ArrayList tagList = new ArrayList();
StringTokenizer st = new StringTokenizer(tags);
while (st.hasMoreElements()) {
tagList.add(st.nextToken());
}
return tagList;
}
/**
* HTML输出内容格式转换
*
* @param content
* @return
*/
public static String formatContent(String content) {
if (content == null)
return "";
String randomStr = String.valueOf(System.currentTimeMillis());
String html = StringUtils.replace(content, " ", randomStr);
html = StringUtils.replace(html, "&", "&");
html = StringUtils.replace(html, "'", "'");
html = StringUtils.replace(html, "\"", """);
html = StringUtils.replace(html, "\t", " ");// 替换跳格
html = StringUtils.replace(html, " ", " ");// 替换空格
html = StringUtils.replace(html, "<", "<");
html = StringUtils.replace(html, ">", ">");
return StringUtils.replace(html, randomStr, " ").trim();
}
/**
* 抽取纯文本信息
*
* @param inputHtml
* @return
*/
public static String extractText(String inputHtml) throws Exception {
StringBuffer text = new StringBuffer();
Parser parser = new Parser();
parser.setInputHTML(new String(inputHtml.getBytes(), "8859_1"));
// Parser parser = Parser.createParser(new
// String(inputHtml.getBytes(),"8859_1"));
// 遍历所有的节点
NodeList nodes = parser.extractAllNodesThatMatch(nodeFilter);
for (int i = 0; i < nodes.size(); i++) {
Node node = nodes.elementAt(i);
text.append(new String(node.toPlainTextString().getBytes("8859_1")));
}
return text.toString();
}
/**
* 判断是不是一个合法的电子邮件地址
*
* @param email
* @return
*/
public static boolean isEmail(String email) {
if (email == null)
return false;
email = email.trim();
if (email.indexOf(' ') != -1)
return false;
int idx = email.indexOf('@');
if (idx == -1 || idx == 0 || (idx + 1) == email.length())
return false;
if (email.indexOf('@', idx + 1) != -1)
return false;
if (email.indexOf('.') == -1)
return false;
return true;
/*
* Pattern emailer; if(emailer==null){ String check =
* "^([a-z0-9A-Z]+[-|\\._]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
* emailer = Pattern.compile(check); } Matcher matcher =
* emailer.matcher(email); return matcher.matches();
*/
}
/**
* 判断字符串是否是一个IP地址
*
* @param addr
* @return
*/
public static boolean isIPAddr(String addr) {
if (isEmpty(addr))
return false;
String[] ips = split(addr, '.');
if (ips.length != 4)
return false;
try {
int ipa = Integer.parseInt(ips[0]);
int ipb = Integer.parseInt(ips[1]);
int ipc = Integer.parseInt(ips[2]);
int ipd = Integer.parseInt(ips[3]);
return ipa >= 0 && ipa <= 255 && ipb >= 0 && ipb <= 255 && ipc >= 0 && ipc <= 255 && ipd >= 0 && ipd <= 255;
} catch (Exception e) {
}
return false;
}
/**
* 二行制转字符串
*
* @param b
* @return
*/
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; b != null && n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
/**
* 二行制偶数判断
*
* @param b
* @return
*/
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
/**
* 大小写无关的字符串替换策略
*
* @param str
* @param src
* @param obj
* @return
*/
public static String replaceIgnoreCase(String str, String src, String obj) {
String l_str = str.toLowerCase();
String l_src = src.toLowerCase();
int fromIdx = 0;
StringBuffer result = new StringBuffer();
do {
int idx = l_str.indexOf(l_src, fromIdx);
if (idx == -1)
break;
result.append(str.substring(fromIdx, idx));
result.append(obj);
fromIdx = idx + src.length();
} while (true);
result.append(str.substring(fromIdx));
return result.toString();
}
/**
* 根据汉字字符获得笔画数,拼音和非法字符默认为0
*
* @param charcator
* @return int
*/
public static int getStrokeCount(char charcator) {
byte[] bytes = (String.valueOf(charcator)).getBytes();
if (bytes == null || bytes.length > 2 || bytes.length <= 0) {
// 错误引用,非合法字符
return 0;
}
if (bytes.length == 1) {
// 英文字符
return 0;
}
if (bytes.length == 2) {
// 中文字符
int highByte = 256 + bytes[0];
int lowByte = 256 + bytes[1];
return getStrokeCount(highByte, lowByte);
}
// 未知错误
return 0;
}
private static int getStrokeCount(int highByte, int lowByte) {
if (highByte < 0xB0 || highByte > 0xF7 || lowByte < 0xA1 || lowByte > 0xFE) {
// 非GB2312合法字符
return -1;
}
int offset = (highByte - 0xB0) * (0xFE - 0xA0) + (lowByte - 0xA1);
return PinyinUtil.gb2312StrokeCount[offset];
}
/**
* 该方法返回一个字符串的拼音,对于要做敏感字 检查时应该一个字一个字来获取其拼音以免无法 得知每个字对应的拼音。
*
* @param word
* @return String
*/
public static String getPinyin(String word) {
String pinyin = "";
for (int i = 0; i < word.length(); i++)
pinyin += getPinyin2(getCode(word.charAt(i)));
return pinyin;
}
/**
* 该方法返回一个字符的DBCS编码值
*
* @param cc
* @return int
*/
protected static int getCode(char cc) {
byte[] bs = String.valueOf(cc).getBytes();
int code = (bs[0] << 8) | (bs[1] & 0x00FF);
if (bs.length < 2)
code = cc;
bs = null;
return code;
}
/**
* 该方法通过DBCS的编码值到哈希表中查询得到对应的拼音串
*
* @param hz
* @return String
*/
protected static String getPinyin2(int hz) {
String py = "";
if (hz > 0 && hz < 160)
py += hz;
// else if (hz < -20319 || hz > -10247);
else if (hz <= -10247 && hz >= -20319) {
PinyinCode pc = null;
int i = PinyinUtil.pinyin.size() - 1;
for (; i >= 0; i--) {
pc = (PinyinCode) PinyinUtil.pinyin.get(i);
if (pc.code <= hz)
break;
}
if (i >= 0)
py = pc.pinyin;
}
return py;
}
/**
* 用户名必须是数字或者字母的结合
*
* @param username
* @return
*/
public static boolean isLegalUsername(String username) {
for (int i = 0; i < username.length(); i++) {
char ch = username.charAt(i);
if (!isAscii(ch) && ch != '.' && ch != '_' && ch != '-' && ch != '+' && ch != '(' && ch != ')' && ch != '*'
&& ch != '^' && ch != '@' && ch != '%' && ch != '$' && ch != '#' && ch != '~' && ch != '-')
return false;
}
return true;
}
/**
* 判断是否是字母和数字的结合
*
* @param name
* @return
*/
public static boolean isAsciiOrDigit(String name) {
for (int i = 0; i < name.length(); i++) {
char ch = name.charAt(i);
if (!isAscii(ch))
return false;
}
return true;
}
public static boolean isAscii(char ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9');
}
/**
* 返回姓名的拼音首字母
*
* @param username
* @return
*/
public static String getTxlUserPinyin(String username) {
if (username.getBytes().length == (2 * username.length())) {
// 纯中文
StringBuffer pinyin = new StringBuffer();
for (int i = 0; i < username.length(); i++) {
String py = StringUtil.getPinyin(String.valueOf(username.charAt(i)));
if (py != null && py.length() > 0)
pinyin.append(py.charAt(0));
else
pinyin.append('V');
}
return pinyin.toString().toUpperCase();
} else if (username.getBytes().length == username.length()) {
int len = (username.length() > 3) ? 3 : username.length();
return username.substring(0, len).toUpperCase();
} else {
StringBuffer pinyin = new StringBuffer();
for (int i = 0; i < username.length(); i++) {
char ch = username.charAt(i);
try {
String py = StringUtil.getPinyin(String.valueOf(ch));
if (py != null && py.length() > 0)
pinyin.append(py.charAt(0));
else
pinyin.append(ch);
} catch (ArrayIndexOutOfBoundsException e) {
}
if (pinyin.length() >= 3)
break;
}
return pinyin.toString().toUpperCase();
}
}
public static double parseDouble(String param) {
double d = 0;
try {
d = Double.parseDouble(param);
} catch (Exception e) {
//
}
return d;
}
public static float parseFloat(String param) {
float f = 0f;
try {
f = Float.parseFloat(param);
} catch (Exception e) {
//
}
return f;
}
public static int parseInt(String param) {
int i = 0;
try {
i = Integer.parseInt(param);
} catch (Exception e) {
i = (int) parseFloat(param);
}
return i;
}
public static long parseLong(String param) {
long l = 0;
try {
l = Long.parseLong(param);
} catch (Exception e) {
l = (long) parseDouble(param);
}
return l;
}
/**
* 编辑之前处理文本框双引号问题
* @param inputString
* @return
*/
public static String processInputBeforeEdit(String inputString) {
return inputString.replace("\"", """);
}
/**
* 更新之前处理文本域空格和换行问题
* @param textAreaString
* @return
*/
public static String processTextAreaBeforeUpdate(String textAreaString) {
return textAreaString.replace(" ", " ").replace("\n", "<br/>");
}
/**
* 编辑之前处理文本域换行问题
* @param textAreaString
* @return
*/
public static String processTextAreaBeforeEdit(String textAreaString) {
return textAreaString.replace("<br/>", "\n");
}
}