/*
* Copyright 2015-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* /licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import ;
import ;
import ;
/**
* 字符串工具类, 继承.类
*
* @author Lusifer
* @version 2013-05-22
*/
public class StringUtils extends org.. {
private static final char SEPARATOR = '_';
private static final String CHARSET_NAME = "UTF-8";
/**
* 转换为字节数组
*
* @param str
* @return
*/
public static byte[] getBytes(String str) {
if (str != null) {
try {
return (CHARSET_NAME);
} catch (UnsupportedEncodingException e) {
return null;
}
} else {
return null;
}
}
/**
* 转换为字节数组
*
* @param str
* @return
*/
public static String toString(byte[] bytes) {
try {
return new String(bytes, CHARSET_NAME);
} catch (UnsupportedEncodingException e) {
return EMPTY;
}
}
/**
* 是否包含字符串
*
* @param str 验证字符串
* @param strs 字符串组
* @return 包含返回true
*/
public static boolean inString(String str, String... strs) {
if (str != null) {
for (String s : strs) {
if ((trim(s))) {
return true;
}
}
}
return false;
}
/**
* 替换掉HTML标签方法
*/
public static String replaceHtml(String html) {
if (isBlank(html)) {
return "";
}
String regEx = "<.+?>";
Pattern p = (regEx);
Matcher m = (html);
String s = ("");
return s;
}
/**
* 替换为手机识别的HTML,去掉样式及属性,保留回车。
*
* @param html
* @return
*/
public static String replaceMobileHtml(String html) {
if (html == null) {
return "";
}
return ("<([a-z]+?)\\s+?.*?>", "<$1>");
}
/**
* 替换为手机识别的HTML,去掉样式及属性,保留回车。
*
* @param txt
* @return
*/
public static String toHtml(String txt) {
if (txt == null) {
return "";
}
return replace(replace((txt), "\n", "<br/>"), "\t", " ");
}
/**
* 缩略字符串(不区分中英文字符)
*
* @param str 目标字符串
* @param length 截取长度
* @return
*/
public static String abbr(String str, int length) {
if (str == null) {
return "";
}
try {
StringBuilder sb = new StringBuilder();
int currentLength = 0;
for (char c : replaceHtml(StringEscapeUtils.unescapeHtml4(str)).toCharArray()) {
currentLength += (c).getBytes("GBK").length;
if (currentLength <= length - 3) {
(c);
} else {
("...");
break;
}
}
return ();
} catch (UnsupportedEncodingException e) {
();
}
return "";
}
public static String abbr2(String param, int length) {
if (param == null) {
return "";
}
StringBuffer result = new StringBuffer();
int n = 0;
char temp;
boolean isCode = false; // 是不是HTML代码
boolean isHTML = false; // 是不是HTML特殊字符,如
for (int i = 0; i < (); i++) {
temp = (i);
if (temp == '<') {
isCode = true;
} else if (temp == '&') {
isHTML = true;
} else if (temp == '>' && isCode) {
n = n - 1;
isCode = false;
} else if (temp == ';' && isHTML) {
isHTML = false;
}
try {
if (!isCode && !isHTML) {
n += (temp).getBytes("GBK").length;
}
} catch (UnsupportedEncodingException e) {
();
}
if (n <= length - 3) {
(temp);
} else {
("...");
break;
}
}
// 取出截取字符串中的HTML标记
String temp_result = ().replaceAll("(>)[^<>]*(<?)", "$1$2");
// 去掉不需要结素标记的HTML标记
temp_result = temp_result.replaceAll("</?(AREA|BASE|BASEFONT|BODY|BR|COL|COLGROUP|DD|DT|FRAME|HEAD|HR|HTML|IMG|INPUT|ISINDEX|LI|LINK|META|OPTION|P|PARAM|TBODY|TD|TFOOT|TH|THEAD|TR|area|base|basefont|body|br|col|colgroup|dd|dt|frame|head|hr|html|img|input|isindex|li|link|meta|option|p|param|tbody|td|tfoot|th|thead|tr)[^<>]*/?>", "");
// 去掉成对的HTML标记
temp_result = temp_result.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>", "$2");
// 用正则表达式取出标记
Pattern p = ("<([a-zA-Z]+)[^<>]*>");
Matcher m = (temp_result);
List<String> endHTML = ();
while (()) {
((1));
}
// 补全不成对的HTML标记
for (int i = () - 1; i >= 0; i--) {
("</");
((i));
(">");
}
return ();
}
/**
* 转换为Double类型
*/
public static Double toDouble(Object val) {
if (val == null) {
return 0D;
}
try {
return (trim(()));
} catch (Exception e) {
return 0D;
}
}
/**
* 转换为Float类型
*/
public static Float toFloat(Object val) {
return toDouble(val).floatValue();
}
/**
* 转换为Long类型
*/
public static Long toLong(Object val) {
return toDouble(val).longValue();
}
/**
* 转换为Integer类型
*/
public static Integer toInteger(Object val) {
return toLong(val).intValue();
}
/**
* 获得i18n字符串
*/
public static String getMessage(String code, Object[] args) {
LocaleResolver localLocaleResolver = (LocaleResolver) ();
HttpServletRequest request = ((ServletRequestAttributes) ()).getRequest();
Locale localLocale = (request);
return ().getMessage(code, args, localLocale);
}
/**
* 获得用户远程地址
*/
public static String getRemoteAddr(HttpServletRequest request) {
String remoteAddr = ("X-Real-IP");
if (isNotBlank(remoteAddr)) {
remoteAddr = ("X-Forwarded-For");
} else if (isNotBlank(remoteAddr)) {
remoteAddr = ("Proxy-Client-IP");
} else if (isNotBlank(remoteAddr)) {
remoteAddr = ("WL-Proxy-Client-IP");
}
return remoteAddr != null ? remoteAddr : ();
}
/**
* 驼峰命名法工具
*
* @return toCamelCase("hello_world") == "helloWorld"
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
* toUnderScoreCase("helloWorld") = "hello_world"
*/
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
s = ();
StringBuilder sb = new StringBuilder(());
boolean upperCase = false;
for (int i = 0; i < (); i++) {
char c = (i);
if (c == SEPARATOR) {
upperCase = true;
} else if (upperCase) {
((c));
upperCase = false;
} else {
(c);
}
}
return ();
}
/**
* 驼峰命名法工具
*
* @return toCamelCase("hello_world") == "helloWorld"
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
* toUnderScoreCase("helloWorld") = "hello_world"
*/
public static String toCapitalizeCamelCase(String s) {
if (s == null) {
return null;
}
s = toCamelCase(s);
return (0, 1).toUpperCase() + (1);
}
/**
* 驼峰命名法工具
*
* @return toCamelCase("hello_world") == "helloWorld"
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
* toUnderScoreCase("helloWorld") = "hello_world"
*/
public static String toUnderScoreCase(String s) {
if (s == null) {
return null;
}
StringBuilder sb = new StringBuilder();
boolean upperCase = false;
for (int i = 0; i < (); i++) {
char c = (i);
boolean nextUpperCase = true;
if (i < (() - 1)) {
nextUpperCase = ((i + 1));
}
if ((i > 0) && (c)) {
if (!upperCase || !nextUpperCase) {
(SEPARATOR);
}
upperCase = true;
} else {
upperCase = false;
}
((c));
}
return ();
}
/**
* 如果不为空,则设置值
*
* @param target
* @param source
*/
public static void setValueIfNotBlank(String target, String source) {
if (isNotBlank(source)) {
target = source;
}
}
/**
* 转换为JS获取对象值,生成三目运算返回结果
*
* @param objectString 对象串
* 例如:
* 返回:!row?'':!?'':!?'':
*/
public static String jsGetVal(String objectString) {
StringBuilder result = new StringBuilder();
StringBuilder val = new StringBuilder();
String[] vals = split(objectString, ".");
for (int i = 0; i < ; i++) {
("." + vals[i]);
("!" + ((1)) + "?'':");
}
((1));
return ();
}
/**
* 通过正则表达式获取内容
*
* @param regex 正则表达式
* @param from 原字符串
* @return
*/
public static String[] regex(String regex, String from) {
Pattern pattern = (regex);
Matcher matcher = (from);
List<String> results = new ArrayList<String>();
while (()) {
for (int i = 0; i < (); i++) {
((i + 1));
}
}
return (new String[]{});
}
}