📄 stringutils.java
字号:
/**
*
*/
package cn.bway.common;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
/**
* @author Kson
*
*/
public class StringUtils {
private final static boolean isISO = !ServerUtils.isGBKsystem();
/**
* 将byte数组转换为表示16进制值的字符串,
* 如:byte[]{8,18}转换为:0813,
* 和public static byte[] hexStr2ByteArr(String strIn)
* 互为可逆的转换过程
* @param arrB 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception 本方法不处理任何异常,所有异常全部抛出
* @author <a href="mailto:zhangji@aspire-tech.com">ZhangJi</a>
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
//每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
//把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
//小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
/**
* 将表示16进制值的字符串转换为byte数组,
* 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的转换过程
* @param strIn 需要转换的字符串
* @return 转换后的byte数组
* @throws Exception 本方法不处理任何异常,所有异常全部抛出
* @author <a href="mailto:zhangji@aspire-tech.com">ZhangJi</a>
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
//两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
/**
*@param str the string need to be parsed
*@param delim the delimiter to seperate
* created by YanFeng at 14/5/2003
*/
public static String[] parseToArray(String str, String delim) {
ArrayList arr = new ArrayList();
StringTokenizer st = new StringTokenizer(str, delim);
while (st.hasMoreTokens()) {
arr.add(st.nextToken());
}
String[] ret = new String[arr.size()];
for (int i = 0; i < arr.size(); i++) {
ret[i] = (String) arr.get(i);
}
return ret;
}
/**
replace a old substring with rep in str
@param str the string need to be replaced
@param old the string need to be removed
@param rep the string to be inserted
@return string replaced
*/
public static String replace(String src, String str1, String str2) {
/*
if ((str==null)||(old==null)||(rep==null))
{//if one is null return ""
return "";
}
int index=str.indexOf(old);
if((index<0)||old.equals(""))
{ //if no old string found or nothing to replace,return the origin
return str;
}
StringBuffer strBuf=new StringBuffer(str);
while(index>=0)
{ //found old part
strBuf.delete(index,index+old.length());
strBuf.insert(index,rep);
index=strBuf.toString().indexOf(old);
}
return strBuf.toString();
*/
StringBuffer str_temp;
int start;
int position;
if (!(src.length() > 0) || str1.equals("")) {
return src;
} else {
str_temp = new StringBuffer().append(src);
start = 0;
position = str_temp.toString().indexOf(str1, start);
while (position > -1 && position < str_temp.toString().length()) {
str_temp = str_temp.replace(position, position + str1.length(),
str2);
start = position + str2.length();
position = str_temp.toString().indexOf(str1, start);
}
return str_temp.toString();
}
}
/**
replace a old substring with rep in str
@param str the string need to be replaced
@param old the string need to be removed
@param rep the string to be inserted
@return string replaced
@CheckItem@ selfbug-yanfeng-20031023 only replace once ocurrence of old
*/
public static String replaceOnlyOnce(String str, String old, String rep) {
if ((old == null) || old.equals("")) { //if old is null or blank return the original string
return str;
}
if ((str == null) || str.equals("")) { //if str is null or blank return the original string
return str;
}
int leftIndex = str.indexOf(old);
if (leftIndex < 0) { //if no old string found so nothing to replace,return the origin
return str;
}
String leftStr = str.substring(0, leftIndex);
String rightStr = str.substring(leftIndex + old.length());
return leftStr + rep + rightStr;
}
/**
* get the string format of a date precise to millisecond
* @param date the input date
* @return the string
* created by yanfeng at13/5/2003
*/
public static String getTimeString(Date date) {
String timePattren = "yyyyMMddHHmmssSSS";
return toString(date, timePattren);
}
/**
convert a date to string according to the format pattern
* @param date input date
* @param pattern format pattern
* @return the formated string
*/
public static String toString(Date date, String pattern) {
SimpleDateFormat fo = new SimpleDateFormat(pattern);
return fo.format(date);
}
/**
* Deal with null strings converting them to "" instead. It also
* invokes String.trim() on the output.
*
* @param foo A String.
* @return A String.
*/
public static final String makeString(String foo) {
return (foo == null ? "" : foo.trim());
}
/**
* Validates that the supplied string is neither <code>null</code>
* nor the empty string.
*
* @param foo The text to check.
* @return Whether valid.
*/
public static final boolean isValid(String foo) {
return (foo != null && foo.length() > 0);
}
/**
* Determine whether a (trimmed) string is empty
*
* @param foo The text to check.
* @return Whether empty.
*/
public static final boolean isEmpty(String foo) {
return (foo == null || foo.trim().length() == 0);
}
/**
* Returns the output of printStackTrace as a String.
*
* @param e A Throwable.
* @return A String.
*/
public static final String stackTrace(Throwable e) {
String foo = null;
try {
// And show the Error Screen.
ByteArrayOutputStream buf = new ByteArrayOutputStream();
e.printStackTrace(new PrintWriter(buf, true));
foo = buf.toString();
} catch (Exception f) {
// Do nothing.
}
return foo;
}
/**
* Returns the output of printStackTrace as a String.
*
* @param e A Throwable.
* @param addPre a boolean to add HTML <pre> tags around the stacktrace
* @return A String.
*/
public static final String stackTrace(Throwable e, boolean addPre) {
if (addPre) {
return "<pre>" + stackTrace(e) + "</pre>";
} else {
return stackTrace(e);
}
}
/**
* Compares two Strings, returns true if their values are the
* same.
*
* @param s1 The first string.
* @param s2 The second string.
* @return True if the values of both strings are the same.
*/
public static boolean equals(String s1, String s2) {
if (s1 == null) {
return (s2 == null);
} else if (s2 == null) {
// s1 is not null
return false;
} else {
return s1.equals(s2);
}
}
public static final int PPKEY_CLASSNAME = 0;
public static final int PPKEY_ID = 1;
public static final int PPKEY_PROPERTY = 2;
/**
* Takes a String of the form substring[substring]subtring and
* returns the 3 substrings
*
* @return a three element String array
*/
public static String[] parseObjectKey(String s) {
String[] p = new String[3];
StringTokenizer st = new StringTokenizer(s, "[]");
int count = st.countTokens();
if (count > 1) {
p[0] = st.nextToken();
p[1] = st.nextToken();
if (count == 3) {
p[2] = st.nextToken();
}
}
return p;
}
/**
* Remove Underscores from a string and replaces first
* Letters with Capitals. foo_bar becomes FooBar
*/
public static String removeUnderScores(String data) {
String temp = null;
StringBuffer out = new StringBuffer();
temp = data;
StringTokenizer st = new StringTokenizer(temp, "_");
while (st.hasMoreTokens()) {
String element = (String) st.nextElement();
out.append(firstLetterCaps(element));
}
return out.toString();
}
/**
* Makes the first letter caps and leaves the rest as is.
*/
public static String firstLetterCaps(String data) {
StringBuffer sbuf = new StringBuffer(data.length());
sbuf.append(data.substring(0, 1).toUpperCase()).append(
data.substring(1));
return sbuf.toString();
}
/**
* Splits the provided CSV text into a list.
*
* @param text The CSV list of values to split apart.
* @param separator The separator character.
* @return The list of values.
*/
public static String[] split(String text, String separator) {
StringTokenizer st = new StringTokenizer(text, separator);
String[] values = new String[st.countTokens()];
int pos = 0;
while (st.hasMoreTokens()) {
values[pos++] = st.nextToken();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -