A more elegant way, that requires JDK1.4, would be this:
import java.util.regex.Pattern; import java.util.regex.Matcher; public class removeIt { public static void main(String argv[]) { String strHtml = "this is\nsome\nstring with\nchr's"; Pattern regPattern = Pattern.compile( "\n", Pattern.DOTALL ); Matcher regMatcher = regPattern.matcher( strHtml ); strHtml = regMatcher.replaceAll( "<P>" ); System.out.println( strHtml ); } } Marc... | -----Original Message----- | From: A mailing list about Java Server Pages specification and reference | [mailto:JSP-INTEREST@;JAVA.SUN.COM]On Behalf Of Kulandaivadivel Duraisamy | Sent: Monday, October 28, 2002 11:57 | To: [EMAIL PROTECTED] | Subject: Re: convert \n to <p> in jsp print statement | | | Hi | | Use the following method to replace all "\n" to "<P>" | | | public String replaceAllValues(String stringValue, String | oldString, String replaceString) | { | String temp1; | String temp2; | for(; stringValue.indexOf(oldString) > -1; stringValue = | temp1 + replaceString + temp2) | { | temp1 = stringValue.substring(0, | stringValue.indexOf(oldString)); | temp2 = | stringValue.substring(stringValue.indexOf(oldString) + | oldString.length(), stringValue.length()); | } | | return stringValue; | | } | | Eg. get the String from the database and call the above method | using the following syntax | | | out.println(replaceAllValues(log_message.get(i).toString(), | "\n","<p>")); %> | | | Regards | | vadivel | | | | | | | ----- Original Message ----- | From: hugo | To: [EMAIL PROTECTED] | Sent: Monday, October 28, 2002 1:20 PM | Subject: convert \n to <p> in jsp print statement | | | Hi | | I have text which I submit to and retrieve from a database. When I | submit to the database, it seems that the line breaks are preserved, but | when I want to retrieve the text to my browser, everything becomes one | long on-going stream of text. Presumably the \n line breaks exist in the | database but they have to be converted to <p> tags for the browser to | display them as line breaks. | | I am using a jsp which has a vector called log_messages to obtain the | messages from the database. After obtaining them, I then display these | messages with the following code. | | <% for (i =0; i < log_message.size(); i ++) { %> | <p> Message: <%out.println(log_message.get(i).toString());%> | <% } %> | | But I would like to convert the newline characters \n (which presumably | are there in some of the message bodies) to <p> statements so new | paragraphs will be showing. Is there an easy way to do this? | | Any help will be greatly appreciated. | | Thanks | | Hugo | | -- | Dr Hugo Bouckaert | Systems and Programming Engineer | | GeoInformatics Exploration Australia P/L | 57 Havelock St | West Perth, WA 6005 | PO Box 1675, West Perth 6872 | | Ph: 61 08 9420 7400 | Fax: 61 08 9226 1299 | | www.geoinformex.com | | ------------------------------------------------------------------------ | This email and any attachments may be confidential or legally | privileged. If you received this message in error or are not the | intended recipient, you should destroy the e-mail message and any | attachments or copies, and you are prohibited from retaining, | distributing, disclosing or using any information contained herein. | Please inform us of the erroneous delivery by return e-mail. Thank you | for your cooperation. | | | ================================================================== | ========= | To unsubscribe: mailto [EMAIL PROTECTED] with body: | "signoff JSP-INTEREST". | For digest: mailto [EMAIL PROTECTED] with body: "set | JSP-INTEREST DIGEST". | Some relevant FAQs on JSP/Servlets can be found at: | | http://archives.java.sun.com/jsp-interest.html | http://java.sun.com/products/jsp/faq.html | http://www.esperanto.org.nz/jsp/jspfaq.jsp | http://www.jguru.com/faq/index.jsp | http://www.jspinsider.com | | ========================= | To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff | JSP-INTEREST". | For digest: mailto [EMAIL PROTECTED] with body: "set | JSP-INTEREST DIGEST". | Some relevant FAQs on JSP/Servlets can be found at: | | http://archives.java.sun.com/jsp-interest.html | http://java.sun.com/products/jsp/faq.html | http://www.esperanto.org.nz/jsp/jspfaq.jsp | http://www.jguru.com/faq/index.jsp | http://www.jspinsider.com | =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at: http://archives.java.sun.com/jsp-interest.html http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.jsp http://www.jguru.com/faq/index.jsp http://www.jspinsider.com