Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing Line Breaks with BR tags

Status
Not open for further replies.

steve0710

Programmer
May 9, 2001
3
0
0
US
I'm new to JSP. I have a field defined as a Text field. The field contains line breaks in it. I am printing this field to the screen and would like to replace the line breaks with HTML BR tags. Could someone help me with the code for this?

I know how to do this in ASP, but haven't been able to figure out how to do it in JSP.

Thanks,
Steve
 
I'm not really quite sure but try this methodology.

Try replacing &quot;\n&quot; with &quot;<BR>&quot;

As far as String documentation goes, visit this site for the Java API documentation.


String resides in the &quot;java.lang&quot; package.



I hope this helped! ;-)
- Casey Winans
 
you can use this code:

public String replace (String str, String pattern, String replace) {
int s = 0;
int e = 0;
StringBuffer result = new StringBuffer();

while ((e = str.indexOf(pattern, s)) >= 0) {
result.append(str.substring(s, e));
result.append(replace);
s = e+pattern.length();
}
result.append(str.substring(s));
return result.toString();
}

compile it into your *.class file, say replace.java -> replace.class

place it into whatever package you use eg. package.replace

in your jsp page:

<jsp:useBean id=&quot;rep&quot; scope=&quot;request&quot; class=&quot;package.replace&quot; />

String blah=&quot;blah\nblah&quot;;

blah=rep.replace(blah,&quot;\n&quot;,&quot;<br>&quot;);

now blah should contain &quot;blah<br>blah&quot;

hope that helps and the circus leaves town...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top