/**
* 字符串长度是否大于limit,汉字长度算2,其他1 大于返回false,小于等于返回true
*
* @param t
* @param limit
* @return
*/
public static boolean checkStringLength(String t, int limit) {
if (StringUtil.isBlank(t)) {
return true;
}
return (t.getBytes().length <= limit);
}
/**
* 替换str在p中出现的
*
* @param str
* @param p
* @return
*/
public static String replaceAllForIcon(String str, Map p) {
if (p == null) {
return str;
}
String ret = str;
try {
for (Iterator it = p.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
String val = (String) p.get(key);
ret = StringUtil.replace(ret, key, "<img src=\"" + val + "\">");
}
} catch (Exception e) {
return ret + "<!-- " + e.getMessage() + " -->";
}
return ret;
}
/**
* 得到日期的格式
*
* @param date
* @param format
* @return
*/
public static String getStringFromDate(Date date, String format) {
if (date == null) {
date = new Date();
}
String s = "";
try {
Locale locale = Locale.CHINESE;
if (format == null) {
format = "yyyy年MM月dd日 HH点mm分ss秒";
}
DateFormat formatter = new SimpleDateFormat(format, locale);
s = formatter.format(date);
} catch (Exception e) {
DateFormat f = DateFormat.getDateInstance();
s = f.format(date);
}
return s;
}
/**
* 得到日期的格式
*
* @param date
* @return
*/
public static String getStringFromDate(Date date) {
return getStringFromDate(date, null);
}
/**
* 将字符串中的http链接转化为html的链接表现 即加上<a href...</a>
*
* @param input
*
* @return
*/
public static String escapeURLsInHTML(String input) {
String output = input;
if (StringUtil.isBlank(input)) {
return input;
}
if (escapeURLsInHTMLPattern == null) {
return input;
}
try {
PatternMatcher matcher = new Perl5Matcher();
PatternMatcherInput mpi = new PatternMatcherInput(input);
int i = 0;
while (matcher.contains(mpi, escapeURLsInHTMLPattern)) {
i++;
// MatchResult result = matcher.getMatch();
// System.out.println(result.group(1));
output = Util.substitute(matcher, escapeURLsInHTMLPattern,
new Perl5Substitution(
"<a href=\"$1\" target=_blank>$1</a>"), input,
Util.SUBSTITUTE_ALL);
// System.out.println("........."+output);
}
return output;
} catch (Exception e) {
return input;
}
}
/**
* 替换文字中的链接
*
* @param input
* @return
*/
public static String escapeUrlHtml(String input) {
String output = escapeURLsInHTML(escapeHTML(input));
return output;
}
/**
* 判断是否是空
*
* @param obj
* @return
*/
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
return StringUtil.isEmpty(obj.toString());
}
/**
* 判断是否非空
*
* @param obj
* @return
*/
public static boolean isNotEmpty(Object obj) {
return !isEmpty(obj);
}
/**
* 检验str中是否包含word
*
* @param str
* @param word
* @return
*/
public static boolean isContain(String str, String word) {
if (str == null) {
return false;
}
if (word == null) {
return true;
}
return str.indexOf(word) >= 0;
}
/**
* 返回str中是否包含word,如果包含,返回word,否则返回null
*
* @param str
* @param word
* @return
*/
public static String getIsContain(String str, String word) {
if (str == null) {
return null;
}
if (word == null) {
return null;
}
if (str.indexOf(word) >= 0) {
return word;
}
return null;
}
/**
* 检验str中是否包含words中的一个
*
* @param str
* @param words
* @return
*/
public static boolean isContain(String str, Set words) {
if (str == null) {
return false;
}
if ((words == null) || (words.size() == 0)) {
return true;
}
for (Iterator it = words.iterator(); it.hasNext();) {
String test = it.next().toString();
if ((test != null) && (test.length() > 0)
&& (str.indexOf(test) >= 0)) {
return true;
}
}
return false;
}
/**
* 替换文字中的关键字
*
* @param str
* @param replacer
* @return
*/
public static String replaceAll(String str, Set replacer) {
if (StringUtil.isBlank(str)) {
return "";
}
if ((replacer == null) || (replacer.size() == 0)) {
return str;
}
Perl5Compiler cp = new Perl5Compiler();
Perl5Matcher m = new Perl5Matcher();
Pattern pt = null;
String output = str;
for (Iterator it = replacer.iterator(); it.hasNext();) {
String[] r = (String[]) it.next();
if (r.length != 2) {
continue;
}
try {
pt = cp.compile(r[0]);
} catch (Exception e) {
continue;
}
if ((pt != null) && m.contains(output, pt)) {
output = Util.substitute(m, pt, new Perl5Substitution(r[1]),
output, Util.SUBSTITUTE_ALL);
}
}
return output;
}
/**
* 返回str中第一个包含的words中的字符,支持正则表达式
*
* @param str
* @param words
* @return
*/
public static String getIsContain(String str, Set words) {
if (str == null) {
return null;
}
if ((words == null) || (words.size() == 0)) {
return null;
}
Perl5Compiler cp = new Perl5Compiler();
Perl5Matcher m = new Perl5Matcher();
Pattern pt = null;
for (Iterator it = words.iterator(); it.hasNext();) {
String test = it.next().toString();
if ((test != null) && (test.length() > 0)) {
try {
pt = cp.compile(test);
} catch (Exception e) {
continue;
}
if (m.contains(str, pt)) {
return m.getMatch().toString();
}
}
}
return null;
}
* 字符串长度是否大于limit,汉字长度算2,其他1 大于返回false,小于等于返回true
*
* @param t
* @param limit
* @return
*/
public static boolean checkStringLength(String t, int limit) {
if (StringUtil.isBlank(t)) {
return true;
}
return (t.getBytes().length <= limit);
}
/**
* 替换str在p中出现的
*
* @param str
* @param p
* @return
*/
public static String replaceAllForIcon(String str, Map p) {
if (p == null) {
return str;
}
String ret = str;
try {
for (Iterator it = p.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
String val = (String) p.get(key);
ret = StringUtil.replace(ret, key, "<img src=\"" + val + "\">");
}
} catch (Exception e) {
return ret + "<!-- " + e.getMessage() + " -->";
}
return ret;
}
/**
* 得到日期的格式
*
* @param date
* @param format
* @return
*/
public static String getStringFromDate(Date date, String format) {
if (date == null) {
date = new Date();
}
String s = "";
try {
Locale locale = Locale.CHINESE;
if (format == null) {
format = "yyyy年MM月dd日 HH点mm分ss秒";
}
DateFormat formatter = new SimpleDateFormat(format, locale);
s = formatter.format(date);
} catch (Exception e) {
DateFormat f = DateFormat.getDateInstance();
s = f.format(date);
}
return s;
}
/**
* 得到日期的格式
*
* @param date
* @return
*/
public static String getStringFromDate(Date date) {
return getStringFromDate(date, null);
}
/**
* 将字符串中的http链接转化为html的链接表现 即加上<a href...</a>
*
* @param input
*
* @return
*/
public static String escapeURLsInHTML(String input) {
String output = input;
if (StringUtil.isBlank(input)) {
return input;
}
if (escapeURLsInHTMLPattern == null) {
return input;
}
try {
PatternMatcher matcher = new Perl5Matcher();
PatternMatcherInput mpi = new PatternMatcherInput(input);
int i = 0;
while (matcher.contains(mpi, escapeURLsInHTMLPattern)) {
i++;
// MatchResult result = matcher.getMatch();
// System.out.println(result.group(1));
output = Util.substitute(matcher, escapeURLsInHTMLPattern,
new Perl5Substitution(
"<a href=\"$1\" target=_blank>$1</a>"), input,
Util.SUBSTITUTE_ALL);
// System.out.println("........."+output);
}
return output;
} catch (Exception e) {
return input;
}
}
/**
* 替换文字中的链接
*
* @param input
* @return
*/
public static String escapeUrlHtml(String input) {
String output = escapeURLsInHTML(escapeHTML(input));
return output;
}
/**
* 判断是否是空
*
* @param obj
* @return
*/
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
return StringUtil.isEmpty(obj.toString());
}
/**
* 判断是否非空
*
* @param obj
* @return
*/
public static boolean isNotEmpty(Object obj) {
return !isEmpty(obj);
}
/**
* 检验str中是否包含word
*
* @param str
* @param word
* @return
*/
public static boolean isContain(String str, String word) {
if (str == null) {
return false;
}
if (word == null) {
return true;
}
return str.indexOf(word) >= 0;
}
/**
* 返回str中是否包含word,如果包含,返回word,否则返回null
*
* @param str
* @param word
* @return
*/
public static String getIsContain(String str, String word) {
if (str == null) {
return null;
}
if (word == null) {
return null;
}
if (str.indexOf(word) >= 0) {
return word;
}
return null;
}
/**
* 检验str中是否包含words中的一个
*
* @param str
* @param words
* @return
*/
public static boolean isContain(String str, Set words) {
if (str == null) {
return false;
}
if ((words == null) || (words.size() == 0)) {
return true;
}
for (Iterator it = words.iterator(); it.hasNext();) {
String test = it.next().toString();
if ((test != null) && (test.length() > 0)
&& (str.indexOf(test) >= 0)) {
return true;
}
}
return false;
}
/**
* 替换文字中的关键字
*
* @param str
* @param replacer
* @return
*/
public static String replaceAll(String str, Set replacer) {
if (StringUtil.isBlank(str)) {
return "";
}
if ((replacer == null) || (replacer.size() == 0)) {
return str;
}
Perl5Compiler cp = new Perl5Compiler();
Perl5Matcher m = new Perl5Matcher();
Pattern pt = null;
String output = str;
for (Iterator it = replacer.iterator(); it.hasNext();) {
String[] r = (String[]) it.next();
if (r.length != 2) {
continue;
}
try {
pt = cp.compile(r[0]);
} catch (Exception e) {
continue;
}
if ((pt != null) && m.contains(output, pt)) {
output = Util.substitute(m, pt, new Perl5Substitution(r[1]),
output, Util.SUBSTITUTE_ALL);
}
}
return output;
}
/**
* 返回str中第一个包含的words中的字符,支持正则表达式
*
* @param str
* @param words
* @return
*/
public static String getIsContain(String str, Set words) {
if (str == null) {
return null;
}
if ((words == null) || (words.size() == 0)) {
return null;
}
Perl5Compiler cp = new Perl5Compiler();
Perl5Matcher m = new Perl5Matcher();
Pattern pt = null;
for (Iterator it = words.iterator(); it.hasNext();) {
String test = it.next().toString();
if ((test != null) && (test.length() > 0)) {
try {
pt = cp.compile(test);
} catch (Exception e) {
continue;
}
if (m.contains(str, pt)) {
return m.getMatch().toString();
}
}
}
return null;
}