package com.test;
/*
*
*
* 字符串處理工具
*
*/
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtil {
public static int getMaxContinuity(String s,char c){
int max=0;
int n=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)==c){
n++;
}else{
n=0;
}
if(n>max){
max=n;
}
}
return max;
}
/**
* 將Double格式化轉化爲String
*
* @param data
* @return
*/
public static String StrDouble(Double data) {
DecimalFormat df = new DecimalFormat("###,###,###");
if (data == null)
return "";
return df.format(data);
}
/**
* 將Double格式化轉化爲String
*
* @param data
* @return
*/
public static String StrDouble1(Double data) {
DecimalFormat df = new DecimalFormat("#");
if (data == null)
return "";
return df.format(data);
}
/**
* 將字符串中的'轉爲''
*
* @param sql
* @return
*/
public static String StrSql(String sql) {
if (sql == null)
return "";
return sql.replaceAll("'", "''");
}
/**
* 判斷str是否爲空,如果str是null返回空,如果str是""返回空
*
* @param str
* @return
*/
public static boolean isNotEmpty(String str) {
if (null == str) return false;
if ("".equals(str.trim())) return false;
return true;
}
/**
* 判斷str是否爲空,如果str是null返回空,如果str是""返回空
*
* @param str
* @return
*/
public static boolean isEmpty(String str) {
if (null == str) return true;
if ("".equals(str.trim())) return true;
return false;
}
/**
* 判斷str是否爲空,如果str是null返回空,如果str是""返回空
*
* @param str
* @return
*/
public static boolean isStr(String[] str, String str1) {
boolean bool = false;
for (int i = 0; i < str.length; i++) {
if (str[i] == str1)
bool = true;
}
return bool;
}
/**
* 去掉HTML標籤
*
* @param input
* @return
*/
public static String escapeHTMLTags(String input) {
//檢查input是否爲空
if (StringUtil.isEmpty(input)) {
return input;
}
//去掉腳本代碼
input = RegexPattern("<script.*?</script>", "", input);
//去掉樣式代碼
input = RegexPattern("<style.*?</style>", "", input);
//去掉註釋
input = RegexPattern("<!--.*?-->", "", input);
//去掉tag
input = RegexPattern("<[^>]+()>", "", input);
input = input.replaceAll(" ", " ");
input = input.replaceAll("”", "\u201D");
input = input.replaceAll("“", "\u201C");
input = input.replaceAll("—", "\u2014");
input = input.replaceAll("<br>", " ");
input = input.replaceAll("<br/>", " ");
input = input.replaceAll("<br />", " ");
return input.trim();
}
/**
* 將content中的符合pattern正則表達式的內容用str來替換
*
* @param pattern
* @param str
* @param content
* @return
*/
public static String RegexPattern(String pattern, String str, String content) {
if (pattern != null && !pattern.equals("")) {
Pattern p = Pattern.compile(pattern, Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(content);
content = m.replaceAll(str);
}
return content;
}
/**
* 正則表達式字符轉義
*
* @param original
* @return
*/
public static String escapeDollarBackslash(String original) {
StringBuffer buffer = new StringBuffer(original.length());
for (int i = 0; i < original.length(); i++) {
char c = original.charAt(i);
if (c == '\\' || c == '$' || c == '.' || c == '*' || c == '?'
|| c == '(' || c == ')' || c == '+' || c == '|' || c == '{'
|| c == '[' || c == '^') {
buffer.append("\\").append(c);
} else {
buffer.append(c);
}
}
return buffer.toString();
}
/**
* 由當前時間生成一個字符串
*
* @return
*/
public static String currentTimeMillis() {
return String.valueOf(System.currentTimeMillis());
}
/**
* 精確截取字符串
*
* @param source 待精確截取的字符串
* @param len 截取長度,長度是以byte爲單位的,一個漢字是2個byte
* @param symbol 省略的信息的字符,如“...”,“>>>”等
* @return
*/
public static String BiteOffString(String source, int len, String symbol) {
int counterOfDoubleByte = 0;
byte b[];
String temp = "";
try {
b = source.getBytes("GBK");
if (b.length <= len)
return source;
for (int i = 0; i < len; i++) {
if (b[i] < 0)
counterOfDoubleByte++;
}
if (counterOfDoubleByte % 2 == 0)
temp = new String(b, 0, len, "GBK") + symbol;
else
temp = new String(b, 0, len - 1, "GBK") + symbol;
}
catch (UnsupportedEncodingException e) {
}
return temp;
}
/**
* lucene搜索時需要去掉的特殊字符
*
* @param src
* @return
*/
public static String refuseLucene(String src) {
String[] refuse = {"+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", "*", "?", ":", "\\"};
for (int i = 0; i < refuse.length; i++) {
src = src.replace(refuse[i], "");
}
return src;
}
/**
* 得到文件擴展名
*
* @param filename
* @return
*/
public static String getExt(String filename) {
String ext = "";
final String invaild = "gif,bmp,jpg,png";
int i = filename.lastIndexOf('.');
if (i != -1) {
ext = filename.substring(i + 1, filename.length()).toLowerCase();
if (invaild.indexOf(ext) == -1)
return "";
else
return ext;
}
return ext;
}
/**
* 對url進行編碼
*
* @param url
* @param encode
* @return
*/
public static String urlEncode(String url, String encode) {
String temp = "";
try {
temp = URLEncoder.encode(url, encode);
}
catch (UnsupportedEncodingException e) {
}
return temp;
}
/**
* 對url進行解碼
*
* @param url
* @param encode
* @return
*/
public static String urlDecode(String url, String encode) {
String temp = "";
try {
temp = URLDecoder.decode(url, encode);
}
catch (UnsupportedEncodingException e) {
}
return temp;
}
/**
* 字符串轉換成數組
*
* @param c
* @return
*/
public static String getWordKey(String url, String c) {
String temp = "";
c = c.replace(" ", " ");
c = c.replace("、", " ");
c = c.replace("/", "");
c = c.replace("|", "");
c = c.trim();
if (c.length() > 0) {
String[] cc = c.split(" ");
for (String a : cc) {
temp = temp + "<a href='" + url + a + ".html' title='標籤' target='_blank'>" + a + "</a> ";
}
}
return temp;
}
/**
* 按照###,###格式化數據
*
* @param d
* @return
*/
public static String format(int d) {
DecimalFormat nf = new DecimalFormat("###,###");
try {
return nf.format(d);
}
catch (Exception e) {
return String.valueOf(d);
}
}
/**
* 數字字符串轉換爲日期類型字符串
*20180112 === 2018-01-12
* @param str
* @return
*/
public static String dateStr(String str) {
StringBuffer newStr = new StringBuffer();
newStr.append(str.substring(0, 4));
newStr.append("-");
newStr.append(str.substring(4, 6));
newStr.append("-");
newStr.append(str.substring(6, 8));
return newStr.toString();
}
/**
* 轉換佔位符
*
* @param str
* @return
*/
public static String changePla(String hql) {
String[] h = hql.split(" ");
StringBuilder sb = new StringBuilder();
Integer num = 0;
for (String s : h) {
if(s.contains("?")){
s=s.replace("?", "?"+num.toString());
num++;
}
sb.append(s+" ");
}
return sb.toString();
}
}