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 Java and 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
 
Hi,

Maybe this isn't a good way but what you can do is loop through the String and replace it manually. Example:-

String temp = "abc \n bcd";
String temp2 = "";
for (int i=0;i<temp.length();i++)
{
if (temp.charAt(i) == '\n')
temp2 = temp2 + &quot;<br>&quot;;
else
temp2 = temp2 + temp.charAt(i);
}
temp = temp2;

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Another way to loop through would be to use a StringBuffer for the replace ...

String text = &quot;This is a test.\nWill this work?\nI do not know.&quot;;

while ( text.indexOf( &quot;\n&quot; ) >= 0 )
text = new StringBuffer( text ).replace(
text.indexOf( &quot;\n&quot; ),
text.indexOf( &quot;\n&quot; ) + 1,
&quot;<br>&quot; ).toString();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top