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!
------------------------------------
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 ();
}
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.