初學jsp時,寫了一些工具函數因為不太會用java下的正則表達式也只能這么寫啦!發出來讓大家批評批評提點意見!有幾個函數不算是自己寫的希望愛挑剌的朋友嘴下留情!我是新手我怕誰,臉皮不行的人水平也上不去呀.嘻嘻..
package mxzc.web.strctrl;
public class stringctrl
{/********************************************
public synchronized string htmlcode(string txtcode) 功能:文本替換
public synchronized string unhtmlcode(string str) 功能:(不完全)反文本替換
public synchronized string unhtmlcodea(string str) 功能:反文本替換
public synchronized boolean emailcheck (string email) 功能:檢查一個字符串是否符合e-mail
public synchronized boolean isemailstr(string email) 功能:檢查一個字符串是否符合e-mail
public synchronized boolean isqqstr(string qq) 功能:檢查一個字符串是否符合qq
public synchronized boolean isnumstr(string num) 功能:檢查一個字符串是否為一數字串
public synchronized string userstrlow(string user) 功能:替換用戶名中不合法的部分
public synchronized boolean userstrchk(string user) 功能:檢查字符串是否符合用戶名法則
public synchronized boolean istelstr(string tel) 功能:檢查字符串是否為tel
public synchronized boolean urlcheck(string url) 功能:檢查字符串是否為url
public synchronized string isotogbk(string iso) 功能:iso9006-1碼轉換為gbk
public synchronized string gbktoiso(string gbk) 功能:gbk碼轉換為iso9006-1
public synchronized string dostrcut(string oldstr,int length) 功能:按漢字長換行(英文按半個字長)
public synchronized string inttodateshow(int datenum) 功能:將1900年至時間的秒數換為日期字符串
public synchronized string nowdateshow() 功能:顯示當前日期
public synchronized java.util.date inttodate(int datenum) 功能:將秒數轉換為日期
public synchronized int datetoint() 功能:將時間換為從1900年至今的秒數
public synchronized int datetoint(java.util.date d) 功能:將時間換為從1900年至時間的秒數
public synchronized string overlengthcut(string str,int length) 功能:截取前幾個字符,單位為漢字字長
public synchronized string replace(string str,string suba,string subb) 功能:字符串替換
*********************************************/
private static final string isostr="iso8859-1";
private static final string gbkstr="gbk";
public stringctrl()
{
}
public synchronized boolean emailcheck (string email)
{
if(email==null)return false;
if(email.length()<6)return false;
if(email.indexof("@")<2)return false;
if(email.indexof(".")<4)return false;
if(email.endswith(".")||email.endswith("@"))return false;
if(email.lastindexof("@")>email.lastindexof(".")-1)return false;
if(email.lastindexof("@")!=email.indexof("@"))return false;
string[] lowstr={"/'","/"","/n","&","/t","/r","<",">","/","http://","#"};
for(int i=0;i<lowstr.length;i++)if(email.indexof("lowstr")>0)return false;
return true;
}
public synchronized boolean isemailstr(string email)
{
if(email==null)return false;
if(email.indexof("@")==-1||email.indexof(".")==-1||email.length()<6)return false;
return true;
}
public synchronized boolean isqqstr(string qq)
{
if(qq==null)return false;
if(qq.length()>12)return false;
if(qq.length()<5)return false;
for(int i=0;i<qq.length();i++)
if(!(((int)qq.charat(i))<=57&&((int)qq.charat(i))>=48))return false;
return true;
}
public synchronized boolean isnumstr(string num)
{
if(num==null)return false;
if(num.length()<1)return false;
for(int i=0;i<num.length();i++)
if(!(((int)num.charat(i))<=57&&((int)num.charat(i))>=48))return false;
return true;
}
public synchronized string userstrlow(string user)
{
string newuserstr=user.trim();
char[] lowstr={'/'','/"','/n','&','/t','/r','<','>','/','//','#'};
for(int i=0;i<lowstr.length;i++)
newuserstr=newuserstr.replace(lowstr[i],'+');
return newuserstr;
}
public synchronized boolean userstrchk(string user)
{
string newuserstr=user.trim();
char[] lowstr={'/'','/"','/n','&','/t','/r','<','>','/','//','#','~','`','!','@','$','%','^','*','(',')','-','_','+','=','|','?',',',';','.'};
for(int i=0;i<lowstr.length;i++)
newuserstr=newuserstr.replace(lowstr[i],'+');
return (user.equals(newuserstr))?true:false;
}
public synchronized boolean istelstr(string tel)
{
if(tel==null)return false;
if(tel.length()<1)return false;
if(tel.length()>32)return false;
for(int i=0;i<tel.length();i++)
if(!(((int)tel.charat(i))<=57&&((int)tel.charat(i))>=48))if(tel.charat(i)!='-')return false;
return true;
}
public synchronized boolean urlcheck(string url)
{
if(url==null)return false;
if(url.length()<10)return false;
string urls=url.tolowercase();
if(!urls.startswith("http://"))return false;
if(url.indexof("<")>0||url.indexof(">")>0)return false;
return true;
}
public synchronized string isotogbk(string iso)throws exception
{
if(iso!=null)return (new string(iso.getbytes(isostr),gbkstr));
if(iso.length()<1)return "";
return null;
}
public synchronized string gbktoiso(string gbk)throws exception
{
if(gbk!=null)return (new string(gbk.getbytes(gbkstr),isostr));
if(gbk.length()<1)return "";
return null;
}
public synchronized string htmlcode(string txtcode)
{
string newstr="";
if(txtcode==null)return "";
newstr=txtcode;
newstr=replace(newstr,"&","&");
newstr=replace(newstr,"/"",""");
newstr=replace(newstr," "," ");
newstr=replace(newstr,"<","<");
newstr=replace(newstr,">",">");
newstr=replace(newstr,"/'","'");
return newstr;
}
public synchronized string unhtmlcode(string str)
{
string newstr="";
if(str==null)return "";
if(str.length()<1)return "";
newstr=str;
newstr=replace(newstr,"&","&");
//newstr=replace(newstr,""","/"");
newstr=replace(newstr," "," ");
newstr=replace(newstr,""","/"");
//newstr=replace(newstr,"<","<");
//newstr=replace(newstr,">",">");
newstr=replace(newstr,"'","/'");
return newstr;
}
public synchronized string unhtmlcodea(string str)
{
string newstr="";
if(str==null)return "";
if(str.length()<1)return "";
newstr=str;
newstr=replace(newstr,"&","&");
newstr=replace(newstr,""","/"");
newstr=replace(newstr," "," ");
newstr=replace(newstr,"<","<");
newstr=replace(newstr,">",">");
newstr=replace(newstr,"'","/'");
return newstr;
}
public synchronized string dostrcut(string oldstr,int length)
{
int i=0;
int j=0;
int k=0;
string newstr="";
if(oldstr==null)return "";
if(length<=0)return "";
for(i=0;i<oldstr.length();i++)
{
if(oldstr.charat(i)=='/n')j=0;
else if(((int)(oldstr.charat(i)))>255)j+=2;
else j++;
if((j/2)>=length)
{
newstr=newstr.concat(oldstr.substring(k,i)+"/n");
k=i;
j=0;
}
}
newstr=newstr.concat(oldstr.substring(k)+"/n");
return newstr;
}
public synchronized string inttodateshow(int datenum)
{
int year=0;
int month=0;
int day=0;
int hour=0;
int minute=0;
int second=0;
string datestr="";
java.util.date d;
d=new java.util.date((long)(datenum)*1000);
java.util.calendar ds=java.util.calendar.getinstance();
ds.settime(d);
year=ds.get(java.util.calendar.year);
month=ds.get(java.util.calendar.month);
day=ds.get(java.util.calendar.date);
hour=ds.get(java.util.calendar.hour_of_day);
minute=ds.get(java.util.calendar.minute);
second=ds.get(java.util.calendar.second);
datestr=integer.tostring(year)+"/"+integer.tostring(1+month)+"/"+integer.tostring(day);
return datestr;
}
public synchronized string nowdateshow()
{
int year=0;
int month=0;
int day=0;
int hour=0;
int minute=0;
int second=0;
string datestr="";
java.util.calendar ds=java.util.calendar.getinstance();
year=ds.get(java.util.calendar.year);
month=ds.get(java.util.calendar.month);
day=ds.get(java.util.calendar.date);
hour=ds.get(java.util.calendar.hour_of_day);
minute=ds.get(java.util.calendar.minute);
second=ds.get(java.util.calendar.second);
datestr=integer.tostring(year)+"/"+integer.tostring(1+month)+"/"+integer.tostring(day);
return datestr;
}
public synchronized java.util.date inttodate(int datenum)
{
int year=0;
int month=0;
int day=0;
string datestr="";
java.util.date d;
d=new java.util.date((long)(datenum)*1000);
return d;
}
public synchronized int datetoint()
{
java.util.date d=null;
long ds=0;
d=new java.util.date();
ds=d.gettime();
return (int)(ds/1000);
}
public synchronized int datetoint(java.util.date d)
{
long ds=0;
ds=d.gettime();
return (int)(ds/1000);
}
public synchronized string overlengthcut(string str,int length)
{
int i=0;
int j=0;
if(str==null)return "";
if(length<0)return "";
if(str.length()<=length)return str;
for(i=0;i<str.length();i++)
{
if(((int)(str.charat(i)))>255)j+=2;
else j++;
if((j/2)>=length)
{
return str.substring(0,i);
}
}
return str;
}
public synchronized string replace(string str,string suba,string subb)
{
string newstr="";
int start=0;
int offset=0;
int subalength=0;
int strlength=0;
if(str==null||suba==null||subb==null)return str;
if(suba.equals(subb))return str;
if(str.length()<suba.length()||str.length()<subb.length())return str;
if(str.length()>0&&suba.length()>0&&subb.length()>0)
{
subalength=suba.length();
strlength=str.length();
while(true)
{
if(str.indexof(suba)<0)break;
if(offset>strlength)break;
start=str.indexof(suba,offset);
if(start<offset)break;
newstr=newstr.concat(str.substring(offset,start));
newstr=newstr.concat(subb);
offset=start+subalength;
}
newstr=newstr.concat(str.substring(offset));
return newstr;
}
else
{
return str;
}
}
}
新聞熱點
疑難解答