初学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","<",">","/","//","#"};
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;
}
}
}
新闻热点
疑难解答