package com.land.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.UUID;
public class StringUtils {
private static final String FOLDER_SEPARATOR = "/";
private static final String WINDOWS_FOLDER_SEPARATOR = "\\";
private static final String TOP_PATH = "..";
private static final String CURRENT_PATH = ".";
private static final char EXTENSION_SEPARATOR = '.';
/**
* Append the given String to the given String array, returning a new array
* consisting of the input array contents plus the given String.
*
* @param array
* the array to append to (can be <code>null</code>)
* @param str
* the String to append
* @return the new array (never <code>null</code>)
*/
public static String[] addStringToArray(String[] array, String str) {
if (ObjectUtils.isEmpty(array)) {
return new String[] { str };
}
String[] newArr = new String[array.length + 1];
System.arraycopy(array, 0, newArr, 0, array.length);
newArr[array.length] = str;
return newArr;
}
/**
* Apply the given relative path to the given path, assuming standard Java
* folder separation (i.e. "/" separators);
*
* @param path
* the path to start from (usually a full file path)
* @param relativePath
* the relative path to apply (relative to the full file path
* above)
* @return the full file path that results from applying the relative path
*/
public static String applyRelativePath(String path, String relativePath) {
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
if (separatorIndex != -1) {
String newPath = path.substring(0, separatorIndex);
if (!relativePath.startsWith(FOLDER_SEPARATOR)) {
newPath += FOLDER_SEPARATOR;
}
return newPath + relativePath;
} else {
return relativePath;
}
}
/**
* Convenience method to return a String array as a CSV String. E.g. useful
* for <code>toString()</code> implementations.
*
* @param arr
* the array to display
* @return the delimited String
*/
public static String arrayToCommaDelimitedString(Object[] arr) {
return arrayToDelimitedString(arr, ",");
}
/**
* Convenience method to return a String array as a delimited (e.g. CSV)
* String. E.g. useful for <code>toString()</code> implementations.
*
* @param arr
* the array to display
* @param delim
* the delimiter to use (probably a ",")
* @return the delimited String
*/
public static String arrayToDelimitedString(Object[] arr, String delim) {
if (ObjectUtils.isEmpty(arr)) {
return "";
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
if (i > 0) {
sb.append(delim);
}
sb.append(arr[i]);
}
return sb.toString();
}
/**
* 将字符串数组转换为sep分隔的字符串
*
* @param arr
* @param sep
* @return
*/
public static String arrToOracleSqlStr(String[] arr, String sep) {
if (arr == null) {
return "";
}
StringBuffer strBuff = new StringBuffer();
int len = arr.length;
for (int i = 0; i < len; i++) {
strBuff.append("'").append(arr[i]).append("'");
if (i < (len - 1)) {
strBuff.append(sep);
}
}
return strBuff.toString();
}
/**
* 将字符串数组转换为sep分隔的字符串
*
* @param arr
* @param sep
* @return
*/
public static String arrToStr(String[] arr, String sep) {
if (arr == null) {
return "";
}
StringBuffer strBuff = new StringBuffer();
int len = arr.length;
for (int i = 0; i < len; i++) {
strBuff.append(arr[i]);
if (i != (len - 1)) {
strBuff.append(sep);
}
}
return strBuff.toString();
}
// 转化byte[]为2位十六进制编码
public static String bytesToHexString(byte[] s) {
StringBuffer str = new StringBuffer("");
char c;
for (int i = 0; i < s.length; i++) {
c = (char) s[i];
int ch = c;
// String s4 = Integer.toHexString(ch);
Integer value = new Integer(c);
String s4 = Integer.toHexString(ch);
if (s4.length() < 4) {
s4 = "000" + s4;
}
s4 = s4.substring(s4.length() - 2);
str.append(s4);
}
return str.toString();
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0xFF);
if (stmp.length() == 1)
hs += ("0" + stmp);
else
hs += stmp;
}
return hs.toUpperCase();
}
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
// 转化字符串为十六进制编码
// public static String toHexString(String s)
// {
// String str="";
// for (int i=0;i<s.length();i++)
// {
// int ch = (int)s.charAt(i);
// String s4 = Integer.toHexString(ch);
// if(s4.length()<4){
// s4="000"+s4;
// s4=s4.substring(s4.length()-4);
// }
// str = str + s4;
// }
// return str;
// }
/**
* Capitalize a <code>String</code>, changing the first letter to upper
* case as per {@link Character#toUpperCase(char)}. No other letters are
* changed.
*
* @param str
* the String to capitalize, may be <code>null</code>
* @return the capitalized String, <code>null</code> if null
*/
public static String capitalize(String str) {
return changeFirstCharacterCase(str, true);
}
private static String changeFirstCharacterCase(String str, boolean capitalize) {
if ((str == null) || (str.length() == 0)) {
return str;
}
StringBuffer buf = new StringBuffer(str.length());
if (capitalize) {
buf.append(Character.toUpperCase(str.charAt(0)));
} else {
buf.append(Character.toLowerCase(str.charAt(0)));
}
buf.append(str.substring(1));
return buf.toString();
}
// 转化字符串为4位十六进制编码
public static String charsToHexString(char[] s) {
StringBuffer str = new StringBuffer("");
for (int i = 0; i < s.length; i++) {
int ch = s[i];
Integer value = new Integer(s[i]);
String s4 = Integer.toHexString(ch);
if (s4.length() < 4) {
s4 = "000" + s4;
s4 = s4.substring(s4.l