encode string for html

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chriskaza81
    New Member
    • Nov 2007
    • 52

    #1

    encode string for html

    hello all!

    well i want to encode a string e.g string1= "<blabla bla>" for html is there a function in java to do that???
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by chriskaza81
    hello all!

    well i want to encode a string e.g string1= "<blabla bla>" for html is there a function in java to do that???
    Can you give more details?
    e.g. Are you talking about this encoding?

    Comment

    • chriskaza81
      New Member
      • Nov 2007
      • 52

      #3
      Originally posted by r035198x
      Can you give more details?
      e.g. Are you talking about this encoding?

      thanks for replay!!! i made a function to do that is this:
      /*------------HTMLencode function----------------*/
      public static String encodeHTML(Stri ng s)
      {
      StringBuffer out = new StringBuffer();
      for(int i=0; i<s.length(); i++)
      {
      char c = s.charAt(i);
      if(c > 127 || c=='"' || c=='<' || c=='>')
      {
      out.append("&#" +(int)c+";");
      }
      else
      {
      out.append(c);
      }
      }
      return out.toString();
      }

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by chriskaza81
        thanks for replay!!! i made a function to do that is this:
        /*------------HTMLencode function----------------*/
        public static String encodeHTML(Stri ng s)
        {
        StringBuffer out = new StringBuffer();
        for(int i=0; i<s.length(); i++)
        {
        char c = s.charAt(i);
        if(c > 127 || c=='"' || c=='<' || c=='>')
        {
        out.append("&#" +(int)c+";");
        }
        else
        {
        out.append(c);
        }
        }
        return out.toString();
        }
        What does this code do when there is a HTML comment:

        <!-- what does it do to this? -->

        Comment

        Working...