以前編寫JSP網站時寫的一些工具函數.

來源:互聯網
上載者:User
js|函數  

初學JSP時,寫了一些工具函數因為不太會用JAVA下的Regex也只能這麼寫啦!發出來讓大家批評批評提點意見!有幾個函數不算是自己寫的希望愛挑剌的朋友嘴下留情!我是新手我怕誰,臉皮不行的人水平也上不去呀.嘻嘻..

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;
}
}
}




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.