[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: converting html special characters



	No, this is not what she needs. URLEncoder does NOT convert a
String into HTML escape characters. URLEncoder makes a String URL-safe,
which means converting spaces into '+', etc.

You actually need to write your own method. Han Bergsten did this in his
great book "JavaServer Pages" (O'Reilly 2001). He wrote a class called
StringFormat that had many methods, including the following:

    public static String toHTMLString(String in) {
        StringBuffer out = new StringBuffer();
        for (int i = 0; in != null && i < in.length(); i++) {
            char c = in.charAt(i);
            if (c == '\'') {
                out.append("&#39;");
            }
            else if (c == '\"') {
                out.append("&#34;");
            }
            else if (c == '<') {
                out.append("&lt;");
            }
            else if (c == '>') {
                out.append("&gt;");
            }
            else if (c == '&') {
                out.append("&amp;");
            }
            else {
                out.append(c);
            }
        }
        return out.toString();
    }

I hope this helps!

Lance England

www.gr.com

 

 

 

	-----Original Message----- 
	From: Kenneth.Wills@equifax.com 
	Sent: Fri 8/3/2001 5:13 PM 
	To: Deron Pardue 
	Cc: ajug-members@ajug.org 
	Subject: Re: converting html special characters
	
	


	Try java.net.URLEncoder
	
	
	
	
"Deron
Pardue"              To:     <ajug-members@ajug.org>
<dpardue@dspi        cc:
n.com>               Subject:     converting html special characters
08/03/01
04:58 PM
Please
respond to
"Deron
Pardue"
Are there any classes that are useful in converting  special html
	characters to their escape sequences...
	
	For instance to translate
	
	    He wrote, " x > 5
	
	into
	
	    He wrote, &quot; x &gt;  5 &quot;
	
	
	
	Thanks!!!