Useful String Functions

Source: Internet
Author: User
Tags functions return string tostring stringbuffer
function | String in JSP, unlike PHP, there are many ready-made string processing functions, in the JSP you need to write, the following is a few commonly used functions, very useful!

-------------------------------
Chinese processing function: GETSTR
public string Getstr (string string) {
try{
String temp_p=string;
Byte[] Temp_t=temp_p.getbytes ("iso8859-1");
String Temp=new string (temp_t);
return temp;
}catch (Exception e) {}
return "NULL";
}

------------------------------------
String substitution function: In line, replace oldstring with newstring
public string Replace (string line, String oldstring, String newstring)
{
int i=0;
if ((I=line.indexof (oldstring, i)) >= 0) {
char [] line2 = Line.tochararray ();
char [] newString2 = Newstring.tochararray ();
int olength = Oldstring.length ();
StringBuffer buf = new StringBuffer (line2.length);
Buf.append (line2, 0, I). Append (NewString2);
i + = olength;
int j = i;
while ((I=line.indexof (oldstring, i)) > 0) {
Buf.append (Line2, J, I-j). Append (NewString2);
i + = olength;
j = i;
}
Buf.append (Line2, J, Line2.length-j);
return buf.tostring ();
}
return line;
}
A practical use is to replace the "\ r \ n" return characters to "<br>"

-------------------------------------------
The following function can replace ' < ' with ' < ', which can be used to publish HTML source code
public string escapehtml (string input) {
if (input==null| | Input.length () ==0)
return input;
StringBuffer buf=new StringBuffer (input.length () +6);
Char ch= ' a ';
for (int i=0;i<input.length (); i++) {
Ch=input.charat (i);
if (ch== ' < ') {
Buf.append ("<");
}
else if (ch== ' > ') {
Buf.append (">");
}
else{
Buf.append (CH);
}
}
return buf.tostring ();
}



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.

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.