package com.cjavapy.utils.util;
import com.alibaba.fastjson.JSONObject;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.regex.Pattern;
public class StringUtil {
public static String formatString(String name) {
String s = "0000000000" + name;
return s.substring(s.length() - 10, s.length());
}
public static String getStringByISO(String name) {
try {
name = new String(name.getBytes("GBK"), "ISO8859_1");
} catch (UnsupportedEncodingException e2) {
}
return name;
}
public static String getFormatUrl(String url) {
if (isNullOrNone(url)) {
return "";
}
if (url.indexOf('?') == -1) {
return url + '?';
} else {
return url + '&';
}
}
public static int lengthByte(String s) {
int length = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) <= 127) {
length++;
} else {
length = length + 2;
}
}
return length;
}
public static int lengthByteInPrepared(String s) {
int length = 0;
if (s == null) {
return length;
}
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) <= 127) {
length++;
} else {
length = length + 3;
}
}
return length;
}
public static boolean isNullOrNone(String s) {
if (s == null || "".equals(s.trim())) {
return true;
}
return false;
}
public static boolean isNullOrNone(Object obj) {
if (obj == null) {
return true;
}
if (obj instanceof String) {
return isNullOrNone((String) obj);
}
return false;
}
public static boolean isNullOrNone(String[] ss) {
if (ss == null || ss.length == 0) {
return true;
}
for (int i = 0; i < ss.length; i++) {
if (ss[i] == null || "".equals(ss[i].trim())) {
return true;
}
}
return false;
}
public static int countChar(String src, char c) {
if (isNullOrNone(src)) {
return 0;
}
int k = 0;
for (int i = 0; i < src.length(); i++) {
if (src.charAt(i) == c) {
k++;
}
}
return k;
}
public static String oracleString(String src) {
if (isNullOrNone(src)) {
return "";
}
return src.replaceAll("'", "''");
}
public static String replace(String source, String find, String rep) {
String result = source;
String begin = "";
String end = "";
int flag;
flag = result.indexOf(find, 0);
while (flag != -1) {
begin = result.substring(0, flag);
end = result.substring(flag + (find.length()), result.length());
result = begin + rep + end;
flag = result.indexOf(find, flag + rep.length());
}
return result;
}
public static String truncate(String s, int begin, int end) {
if (isNullOrNone(s)) {
return "";
}
if (begin < 0) {
return "";
}
if (begin >= end) {
return "";
}
s = s.trim();
if (begin >= s.length()) {
return "";
}
if (end >= s.length()) {
return s;
} else {
return s.substring(begin, end);
}
}
public static boolean isNumber(String str) {
if (null == str || "".equals(str)) {
return false;
} else {
return Pattern.matches("^[0-9]+$", str);
}
}
public static boolean isInteger(String str) {
try {
double value = Double.parseDouble(str);
if (value == Math.ceil(value)) {
return true;
}
} catch (Exception e) {
return false;
}
return false;
}
public static int realLength(String s) {
if (null == s || "".equals(s)) {
return 0;
} else {
return s.getBytes().length;
}
}
public static boolean isUnlawfulChar(String s) {
if (null == s || "".equals(s)) {
return false;
} else {
return Pattern.matches("^[^`~@#\\$%\\^&\\*\\(\\)=\\!\\+\\\\/\\|<>\\?;\\:\\.'\"\\{\\}\\[\\]??, ]*$", s);
}
}
public static boolean isLetter(String str) {
if (null == str || "".equals(str)) {
return false;
} else {
return Pattern.matches("^[A-Za-z]+$", str);
}
}
public static String nvl(String source) {
return (source != null) ? source.trim() : "";
}
public static String nvl(String source, String defaultString) {
return (source != null) ? source.trim() : defaultString;
}
public static String reverseString(String str) {
StringBuffer resultStringBuffer = new StringBuffer();
String[] allStr = str.split(" ");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < allStr.length; i++) {
if (meetReverseCondition(allStr[i])) {
sb.delete(0, sb.length());
resultStringBuffer.append(sb.append(allStr[i]).reverse()).append(" ");
} else {
resultStringBuffer.append(allStr[i]).append(" ");
}
}
return resultStringBuffer.deleteCharAt(resultStringBuffer.length() - 1).toString();
}
private static boolean meetReverseCondition(String str) {
return (str.charAt(0) == '*') && (str.length() > 1) && Character.isLetterOrDigit(str.charAt(1));
}
public static String splitStr(String str, int maxLength) {
if (maxLength <= 0) {
return "";
}
String result = str;
byte[] bytes = str.getBytes();
if (bytes.length > maxLength) {
result = new String(bytes, 0, maxLength);
if (!result.substring(result.length() - 1).equals(str.substring(result.length() - 1, result.length()))) {
result = new String(bytes, 0, maxLength - 1);
}
}
return result;
}
public static boolean isExists(String filterStr, String id) {
String[] filterTag = filterStr.split(",");
for (int i = 0; i < filterTag.length; i++) {
if (id.equals(filterTag[i])) {
return true;
}
}
return false;
}
public static String getTime(Date dt, String format) {
String time = null;
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
time = df.format(dt);
return time;
}
public static String[] stringToArray(String str, String start, String end, String split) {
if (null == str || "".equals(str))
return null;
if (str.indexOf(start) == -1 || str.indexOf(end) == -1 || str.indexOf(split) == -1)
return new String[]{str};
return str.substring(str.indexOf(start) + 1, str.indexOf(end)).split(split);
}
public static boolean isEmpty(String str) {
return str == null || "".equals(str.trim());
}
public static Boolean isJsonObject(String msg) {
try {
JSONObject.parseObject(msg);
} catch (Exception e) {
return false;
}
return true;
}
public static String getSubStr(String src, int length) {
if (StringUtil.isEmpty(src)) {
return null;
}
byte[] b = src.getBytes();
if (b.length > length) {
byte[] s = new byte[length];
for (int i = 0; i < length; i++) {
s[i] = b[i];
}
return new String(s);
} else {
return src;
}
}
public static String truncateMessage(String description, int length) {
org.springframework.util.Assert.state(length > 0);
if ((description != null) && (description.length() > length)) {
return description.substring(0, length);
}
return description;
}
public static boolean jsonHasKayAndValue(JSONObject param, String key) {
if (param == null) {
return false;
}
if (!param.containsKey(key)) {
return false;
}
if (isEmpty(param.getString(key))) {
return false;
}
return true;
}
public static boolean jsonHasKayAndValue(String param, String key) {
JSONObject paramObj = null;
try {
paramObj = JSONObject.parseObject(param);
return jsonHasKayAndValue(paramObj, key);
} catch (Exception e) {
return false;
}
}
public static String delHtmlTag(String str){
String newstr = "";
newstr = str.replaceAll("<[.[^>]]*>","");
newstr = newstr.replaceAll(" ", "");
return newstr;
}
}