package com.hunter.fastandroid.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.util.Log;
/**
* 字符串操作工具类
* @author Ht
*
*/
public class MyStringUtils {
private final static Pattern EMAILER = Pattern
.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
private final static ThreadLocal<SimpleDateFormat> DATE_FORMATER = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
/**
* 字符串截取
*
* @param str
* @param length
* @return
* @throws Exception
*/
public static String subString(String str, int length) throws Exception {
byte[] bytes = str.getBytes("Unicode");
int n = 0; // 表示当前的字节数
int i = 2; // 要截取的字节数,从第3个字节开始
for (; i < bytes.length && n < length; i++) {
// 奇数位置,如3、5、7等,为UCS2编码中两个字节的第二个字节
if (i % 2 == 1) {
n++; // 在UCS2第二个字节时n加1
} else {
// 当UCS2编码的第一个字节不等于0时,该UCS2字符为汉字,一个汉字算两个字节
if (bytes[i] != 0) {
n++;
}
}
}
// 如果i为奇数时,处理成偶数
if (i % 2 == 1) {
// 该UCS2字符是汉字时,去掉这个截一半的汉字
if (bytes[i - 1] != 0)
i = i - 1;
// 该UCS2字符是字母或数字,则保留该字符
else
i = i + 1;
}
return new String(bytes, 0, i, "Unicode");
}
/**
*
* @param input
* @return
*/
public static String toDBC(String input) {
char[] c = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 12288) {
c[i] = (char) 32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char) (c[i] - 65248);
}
return new String(c);
}
/**
* 计算微博内容的长度 1个汉字 == 两个英文字母所占的长度 标点符号区分英文和中文
*
* @param c
* 所要统计的字符序列
* @return 返回字符序列计算的长度
*/
public static long calculateWeiboLength(CharSequence c) {
double len = 0;
for (int i = 0; i < c.length(); i++) {
int temp = (int) c.charAt(i);
if (temp > 0 && temp < 127) {
len += 0.5;
} else {
len++;
}
}
return Math.round(len);
}
/**
* 分割字符串
*
* @param str
* String 原始字符串
* @param splitsign
* String 分隔符
* @return String[] 分割后的字符串数组
*/
public static String[] split(String str, String splitsign) {
int index;
if (str == null || splitsign == null)
return null;
ArrayList<String> al = new ArrayList<String>();
while ((index = str.indexOf(splitsign)) != -1) {
al.add(str.substring(0, index));
str = str.substring(index + splitsign.length());
}
al.add(str);
return (String[]) al.toArray(new String[0]);
}
/**
* 替换字符串
*
* @param from
* String 原始字符串
* @param to
* String 目标字符串
* @param source
* String 母字符串
* @return String 替换后的字符串
*/
public static String replace(String from, String to, String source) {
if (source == null || from == null || to == null)
return null;
StringBuffer bf = new StringBuffer("");
int index = -1;
while ((index = source.indexOf(from)) != -1) {
bf.append(source.substring(0, index) + to);
source = source.substring(index + from.length());
index = source.indexOf(from);
}
bf.append(source);
return bf.toString();
}
/**
* 替换字符串,能能够在HTML页面上直接显示(替换双引号和小于号)
*
* @param str
* String 原始字符串
* @return String 替换后的字符串
*/
public static String htmlencode(String str) {
if (str == null) {
return null;
}
return replace("\"", """, replace("<", "<", str));
}
/**
* 替换字符串,将被编码的转换成原始码(替换成双引号和小于号)
*
* @param str
* String
* @return String
*/
public static String htmldecode(String str) {
if (str == null) {
return null;
}
return replace(""", "\"", replace("<", "<", str));
}
private static final String _BR = "<br/>";
/**
* 在页面上直接显示文本内容,替换小于号,空格,回车,TAB
*
* @param str
* String 原始字符串
* @return String 替换后的字符串
*/
public static String htmlshow(String str) {
if (str == null) {
return null;
}
str = replace("<", "<", str);
str = replace(" ", " ", str);
str = replace("\r\n", _BR, str);
str = replace("\n", _BR, str);
str = replace("\t", " ", str);
return str;
}
/**
* 返回指定字节长度的字符串
*
* @param str
* String 字符串
* @param length
* int 指定长度
* @return String 返回的字符串
*/
public static String toLength(String str, int length) {
if (str == null) {
return null;
}
if (length <= 0) {
return "";
}
try {
if (str.getBytes("GBK").length <= length) {
return str;
}
} catch (Exception ex) {
}
StringBuffer buff = new StringBuffer();
int index = 0;
char c;
length -= 3;
while (length > 0) {
c = str.charAt(index);
if (c < 128) {
length--;
} else {
length--;
length--;
}
buff.append(c);
index++;
}
buff.append("...");
return buff.toString();
}
/**
* 获取url的后缀名
*
* @param urlString
* @return
*/
public static String getUrlFileName(String urlString) {
String fileName = urlString.substring(urlString.lastIndexOf("/"));
fileName = fileName.substring(1, fileName.length());
if (fileName.equalsIgnoreCase("")) {
Calendar c = Calendar.getInstance();
fileName = c.get(Calendar.YEAR) + "" + c.get(Calendar.MONTH) + ""
+ c.get(Calendar.DAY_OF_MONTH) + ""
+ c.get(Calendar.MINUTE);
}
return fileName;
}
public static String replaceSomeString(String str) {
String dest = "";
try {
if (str != null) {
str = str.replaceAll("\r", "");
str = str.replaceAll(">", ">");
str = str.replaceAll("“", "“");
str = str.replaceAll("”", "”");
str = str.replaceAll("'", "'");
str = str.replaceAll(" ", "");
str = str.replaceAll("<br\\s*/>", "\n");
str = str.replaceAll(""", "\"");
str = str.replaceAll("<", "<");
str = str.replaceAll("‘", "《");
str = str.replaceAll("’", "》");
str = str.replaceAll("·", "·");
str = str.replace("—", "—");
str = str.replace("…", "…");
str = str.replace("&", "×");
str = str.replaceAll("\\s*", "");
str = str.trim();
str = str.replaceAll("<p>", "\n ");
str = str.replaceAll("</p>", "");
str = str.replaceAll("<div.*?>", "\n ");
str = str.replaceAll("</div>", "");
dest = str;
}
} catch (Exception e) {
// TODO: handle exception
}
return dest;
}
/**
* 清除文本里面的HTML标签
*
* @param htmlStr
* @return
*/
public static String delHTMLTag(String htmlStr) {
String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
Log.v("htmlStr", htmlStr);
try {
Pattern p_script = Pattern.compile(regEx_script,
Pattern.CASE_INSENSITIVE);
Matcher m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签
Pattern p_style = Pat