SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member sankrish's Avatar
    Join Date
    May 2008
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    equivalent function for nl2br() in JSP

    Hi,

    In PHP there is a function nl2br() to format text,this function replaces new-line charaters "/n" with html line break tags "<br/>".
    Is there is any function in JSP to do this operation?

    Regards,
    SAN

    Thanks in advance

  2. #2
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this for one line or a whole page?

    You can try the string replaceAll method. That should take care of it.

    You could write a method nl2br

    Code:
    public static String nl2br(String s) {
      return s.replaceAll("\n","<br/>");
    }
    
    // usage
    nl2br("some\ntext");
    
    // this will return "some<br/>text";
    You can also look at the Apache commons lang library. It has a class called StringUtils which may have something like this already but I'm not sure.

  3. #3
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    You'll need a few more slashes to get this to work as the slash is also the escape character for both the Java compiler and Regular Expressions. (RegEx is what is used to make replaceAll work)

    Specifically: \\\\n

    Javac will see \\\\n as \\n
    RegEx will see \\n as \n

    (slashes considered escapes by javac and regex have been underlined)

  4. #4
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here a code that replace \n to <br>

    public String NLtoBR(String msg)
    {
    msg = msg.replace("\n","<br>");
    return msg;
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •