package com.wasu.as.sys;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class StringUtil {
public static final String XML_TAG_AND = "\7";
public static final String replace( String line, String oldString, String newString )
{
if (line == null) {
return null;
}
int i=0;
if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) {
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while( ( i=line.indexOf( oldString, i ) ) > 0 ) {
buf.append(line2,j,i-j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
public static final boolean isEmptyString(String s){
if(s==null) return true;
if("".equals(s.trim())) return true;
return false;
}
public static final String convertNumberToString(Number value) {
DecimalFormat formatter = new DecimalFormat();
formatter.setGroupingUsed(false);
String result = formatter.format(value.doubleValue());
return result;
}
/**
* ��Stringѹ��
* example:com.jbbis.aic.alteration.entity.AlterRequisition ��
* integrateOpinion,getIntegrateOpinionInBytes(),setIntegrateOpinionInBytes
*/
public static byte[] zipString(String s){
if(s==null) return null;
try {
byte[] bytes = s.getBytes("GBK");
BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(bytes));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(baos));
byte[] buffer = new byte[1024];
int len = 0;
while( (len=in.read(buffer)) != -1){
out.write(buffer,0,len);
}
in.close();
out.close();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* ��ѹString
*/
public static String unzipString(byte[] bytes){
if(bytes==null||bytes.length==0) return null;
try {
BufferedInputStream in = new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(bytes)));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream out = new BufferedOutputStream(baos);
byte[] buffer = new byte[1024];
int len = 0;
while( (len=in.read(buffer)) != -1){
out.write(buffer,0,len);
}
in.close();
out.close();
bytes = baos.toByteArray();
return new String(bytes,"GBK");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String stringToHtmlFormat(String s){
if(s==null) return null;
String temp = replace(s,"\n","<br>");
temp = replace(temp,"\t"," ");
return replace(temp," "," ");
}
/**
* ���ַ���п��滻�����������ǿ��ַ�����ԭ�ַ�����ǿ�(null)�����ؿ��ַ�("")
* @param s
* @return
* @author shenyf
*/
public static String toStringNullReplaced(String s){
return s==null?"":s;
}
/**
* ��l�����ַ�ת���������п��滻�����������ǿ�l�������ַ��ʾ��lֵ������ǿ�(null)�����ؿ��ַ�("")
* @param l
* @return
* @author shenyf
*/
public static String toStringNullReplaced(Long l){
return l==null?"":l.toString();
}
/**
* ��i�����ַ�ת���������п��滻�����������ǿ�i�������ַ��ʾ��iֵ������ǿ�(null)�����ؿ��ַ�("")
* @param i
* @return
* @author shenyf
*/
public static String toStringNullReplaced(Integer i){
return i==null?"":i.toString();
}
public static String[] split(String origin, String limit) {
List container = new ArrayList();
String org = new String(origin);
while (org.indexOf(limit) >= 0) {
String str = org.substring(0, org.indexOf(limit));
if(str.length() >=0) {
container.add(str);
}
org = org.substring(org.indexOf(limit) + limit.length());
}
if(org.length() > 0) {
container.add(org);
}
String[] result = new String[container.size()];
container.toArray(result);
return result;
}
/**
* ���ַ������ݹ���ƴ���ַ���{"abc","def","ghi"}ת����'abc','def','ghi'ֻ�����toString(arr,"'",",")
* @param _arr �ַ�
* @param _wrapper �ַ�}�ߵİ���ַ�
* @param _separator �ָ��
* @return �������Ϊ�ջ�Ϊ0������null
*/
public static String toString(String[] _arr,String _wrapper,String _separator){
if(_arr==null || _arr.length<=0){
return null;
}
return toString(_arr, 0, _arr.length, _wrapper, _separator);
}
/**
* ���ַ������ݹ���ƴ���ַ���{"abc","def","ghi"}ת����'abc','def','ghi'ֻ�����toString(arr,0,3,"'",",")
* @param _arr �ַ�
* @param _fromIndex ��һ��Ԫ��(��)���±�
* @param _toIndex ���һ��Ԫ��(����)���±ꡣ�� _toIndex > _arr.length�����൱�� _toIndex = _arr.length
* @param _wrapper �ַ�}�ߵİ���ַ�
* @param _separator �ָ��
* @return �������Ϊ�ջ�Ϊ0������null
*/
public static String toString(String[] _arr, int _fromIndex, int _toIndex, String _wrapper,String _separator){
if(_arr==null || _arr.length<=0){
return null;
}
StringBuffer strBuff = new StringBuffer();
for(int i = _fromIndex;i < _arr.length && i < _toIndex;i++){
strBuff.append(_wrapper+_arr[i]+_wrapper+_separator);
}
if (strBuff.length() > 0) {
strBuff.deleteCharAt(strBuff.length() - 1);
}
return strBuff.toString();
}
/**
* ��_arr�в���ƥ����ַ�
* @param _tobeFound ƥ���ַ�
* @param _arr
* @return ����ҵ�������true������false
*/
public static boolean findIn(String _tobeFound,String[] _arr){
if(_arr==null || _arr.length<=0){
return false;
}
for(int i=0;i<_arr.length;i++){
if(_arr[i].equals(_tobeFound)){
return true;
}
}
return false;
}
/**
* ��һ��Setת�������顣Set�е�Ԫ��ΪString��
* @param _set
* @return
*/
public static String[] toArray(Set _set){
if(_set==null || _set.size()<=0){
return null;
}
String[] arr=new String[_set.size()];
int i=0;
for(Iterator itr=_set.iterator();itr.hasNext();){
arr[i++]=(String)itr.next();
}
return arr;
}
/**
* ȥ���ַ��еİ�ǻ�ȫ�ǿո�
* @param s ��Ҫ������ַ�
* @return �������ַ�
*/
public static String trim(String s) {
if (s == null || s.length() == 0) {
return s;
}