Java HTML Encode htmlEncode(String s)

Here you can find the source of htmlEncode(String s)

Description

Helper method to html encode a string.

License

Open Source License

Parameter

Parameter Description
s string to encode

Return

encoded version

Declaration

public static String htmlEncode(String s) 

Method Source Code

//package com.java2s;
/*//  w  ww .  j  ava 2 s. c  o m
 * Copyright 2009 Yodlee, Inc.  All Rights Reserved.  Your use of this code 
 * requires a license from Yodlee.  Any such license to this code is 
 * restricted to evaluation/illustrative purposes only. It is not intended 
 * for use in a production environment, and Yodlee disclaims all warranties 
 * and/or support obligations concerning this code, regardless of the terms 
 * of any other agreements between Yodlee and you."
 */

public class Main {
    /**
     * Helper method to html encode a string.  Used for dumping the form
     * output.
     *
     * @param s string to encode
     * @return encoded version
     */
    public static String htmlEncode(String s) {
        StringBuffer sb = new StringBuffer();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            switch (c) {
            case '<':
                sb.append("&lt;");
                break;
            case '>':
                sb.append("&gt;");
                break;
            case '&':
                sb.append("&amp;");
                break;
            case '"':
                sb.append("&quot;");
                break;
            default:
                sb.append(c);
                break;
            }
        }

        return sb.toString();
    }
}

Related

  1. htmlEncode(String input)
  2. htmlEncode(String input)
  3. htmlEncode(String s)
  4. htmlEncode(String s)
  5. htmlEncode(String s)
  6. htmlEncode(String s)
  7. htmlEncode(String s)
  8. HTMLEncode(String s)
  9. htmlEncode(String s, boolean encodeSpecialChars)