Some of the tool functions that were written when you wrote a JSP web site previously

Source: Internet
Author: User
Tags date final functions integer regular expression string tostring trim
js| function

Beginners JSP, wrote a number of tool functions because it is not very good to use the regular expression in Java can only be so written! Send out to let everyone criticize and criticize and make some comments! There are a few functions that are not written by their own hope to love the friends of the mouth! I am the novice I fear who, the shameless person level also does not go.

Package Mxzc.web.strctrl;
public class Stringctrl
{/********************************************
Public synchronized string Htmlcode (String txtcode) feature: Text substitution
Public synchronized string Unhtmlcode (String str) feature: (incomplete) anti-text substitution
Public synchronized string Unhtmlcodea (String str) feature: Anti-text substitution
Public synchronized Boolean Emailcheck (string email) function: Check whether a string meets e-mail
Public synchronized Boolean isemailstr (string email) function: Check whether a string meets e-mail
Public synchronized Boolean ISQQSTR (String qq) function: Check if a string matches QQ
Public synchronized Boolean isnumstr (String num) feature: Check whether a string is a number string
Public synchronized string Userstrlow (string user) feature: Replace an illegal part of a user name
Public synchronized Boolean userstrchk (string user) function: Check whether the string conforms to the username rule
Public synchronized Boolean ISTELSTR (String tel) feature: Check whether the string is tel
Public synchronized Boolean urlcheck (string url) feature: Check whether the string is a URL
Public synchronized string ISOTOGBK (String iso) feature: iso9006-1 code conversion to GBK
Public synchronized string Gbktoiso (String gbk) feature: GBK code conversion to Iso9006-1
Public synchronized string Dostrcut (string oldstr,int length) function: line by length of Chinese characters (half word in English)
Public synchronized string inttodateshow (int datenum) feature: Swap the number of seconds from 1900 to a date string
Public synchronized String nowdateshow () feature: Show current date
Public synchronized java.util.Date inttodate (int datenum) Feature: Converts the number of seconds to a date
public synchronized int datetoint () feature: Swap time for seconds from 1900 to present
public synchronized int Datetoint (java.util.Date d) feature: Swap time for seconds from 1900 to time
Public synchronized string Overlengthcut (string str,int length) feature: Intercepts the first few characters, the unit is the word length of Chinese characters
Public synchronized string Replace (string str,string suba,string Subb) feature: string substitution
*********************************************/
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 (".") &LT;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 to 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 (offset) (str.substring);
return newstr;
}
Else
{
return str;
}
}
}



Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.