//字符串初始化
public String toString(String str){
if(str == null){
str = "";
}
if(str.equals("null")){
str = "";
}
str = str.trim();
return str;
}
//編碼轉換
public String toGBK(String str){
try{
if(str == null){
str = "";
}else{
str = new String(str.getBytes("ISO-8859-1"),"GBK");
}
}catch(Exception e){
System.out.println("DealString::toGBK(String)運行時出錯:"+e;
}
}
//UTF轉換
public String to Utf8String(String src){
byte[] b = src.getBytes();
char[] c = new char[b.length];
for(int i = 0; i < b.length; i++){
c[i] = (char)(b[i]&0x00FF);
}
return new String(c);
}
//字符分隔
public String[] splitStr(String str,char c){
str += c;
int n = 0;
for(int i = 0; i < str.length();i++){
if(str.charAt(i) == c){
n ++;
}
}
String out[] = new String[n];
for(int i = 0; i < n; i++){
int index = str.indexOf(c);
out[i] = str.substring(0,index);
str = str.substring(index+1),str.length());
}
return out;
}
//取得系統時間
public String getDateTime(){
java.text.SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = f.format(new java.util.Date());
return time;
}
//字符串替換
public String Replace(String source,String oldString,String newString){
StringBuffer output = new StringBuffer();
int lengthOfSource = source.length();
int lengthOfOld = oldString.length();
int posStart = 0;
int pos;
while((pos = source.indexOf(oldString,posStart)) >= 0){
output.append(source.substring(posStart,pos));
output.append(newString);
posStart = pos + lengthOfOld;
}
if(posStart < lengthOfSource){
output.append(source.substring(posStart));
}
return output.toString();
}
//日期之差
public long getDaysInterval(Date d1,Date d2){
return (d2.getTime() - d1.getTime())/86400000;
}
//字符串右格式化
public String toLengthStr(String instr,int len){
int n = instr.length();
for(int i = 0; i < (len - n); i++){
instr = " " + instr;
}
return instr;
}
//字符串左格式化
public String toLengthStr(String instr,int len){
int n = instr.length();
for(int i = 0; i < (len - n); i++){
instr = instr + " ";
}
return instr;
}
//字符串位長度
public int byteLength(String str){
return ((str.getBytes()).length);
}
//字符串位截取
public String strByteCopy(String str,int nEnd){
if(nEnd == 0){
return "";
}
byte[] byteStr = str.getBytes();
int k = byteStr.length;
String strSub = new String(byteStr,0,End<k?nEnd:k)
if(strSub.length() == 0){
strSub = new String(byteStr,0,nEnd-1);
}
return strSub;
}