Java HTML Encode htmlEncode(String txt)

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

Description

html Encode

License

Apache License

Declaration

public static String htmlEncode(String txt) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    public static String htmlEncode(String txt) {
        if (null != txt) {
            txt = txt.replace("&", "&").replace("&", "&").replace(""", """)
                    .replace("\"", "&quot;").replace("&amp;lt;", "&lt;").replace("<", "&lt;")
                    .replace("&amp;gt;", "&gt;").replace(">", "&gt;").replace("&amp;nbsp;", "&nbsp;");
        }/*ww w  .  j a v  a  2s .c  om*/
        return txt;
    }

    public static final String replace(String line, String oldString, String newString) {
        if (null == line && "".equals(line)) {
            return null;
        }
        int i = 0;
        if ((i = line.indexOf(oldString, i)) >= 0) {
            char[] line2 = line.toCharArray();
            char[] newString2 = newString.toCharArray();
            int len = oldString.length();
            StringBuffer buffer = new StringBuffer(line2.length);
            buffer.append(line2, 0, i).append(newString2);
            i += len;
            int j = i;
            while (((i = line.indexOf(oldString, i))) > 0) {
                buffer.append(line2, j, i - j).append(newString2);
                i += len;
                j = i;
            }
            buffer.append(line2, j, line2.length - j);
            return buffer.toString();
        }
        return line;
    }

    public static String replace(String text, String searchString, String replacement, int max) {
        if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0) {
            return text;
        }
        int start = 0;
        int end = text.indexOf(searchString, start);
        if (end == -1) {
            return text;
        }
        int replLength = searchString.length();
        int increase = replacement.length() - replLength;
        increase = (increase < 0 ? 0 : increase);
        increase *= (max < 0 ? 16 : (max > 64 ? 64 : max));
        StringBuffer buf = new StringBuffer(text.length() + increase);
        while (end != -1) {
            buf.append(text.substring(start, end)).append(replacement);
            start = end + replLength;
            if (--max == 0) {
                break;
            }
            end = text.indexOf(searchString, start);
        }
        buf.append(text.substring(start));
        return buf.toString();
    }

    public static int indexOf(String str, char strChar) {
        if (isEmpty(str)) {
            return -1;
        }
        return str.indexOf(strChar);
    }

    /**
     * <pre>
     * ExmayStringUtils.indexOf(null, *)          = -1
     * ExmayStringUtils.indexOf(*, null)          = -1
     * ExmayStringUtils.indexOf("", "")           = 0
     * ExmayStringUtils.indexOf("aabaabaa", "a")  = 0
     * ExmayStringUtils.indexOf("aabaabaa", "b")  = 2
     * ExmayStringUtils.indexOf("aabaabaa", "ab") = 1
     * ExmayStringUtils.indexOf("aabaabaa", "")   = 0
     * </pre>
     * 
     * @param str
     * @param searchStr
     * @return
     */
    public static int indexOf(String str, String searchStr) {
        if (str == null || searchStr == null) {
            return -1;
        }
        return str.indexOf(searchStr);
    }

    public static int indexOf(String str, char searchChar, int startPos) {
        if (isEmpty(str)) {
            return -1;
        }
        return str.indexOf(searchChar, startPos);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0 || str.equals("") || str.matches("\\s*");
    }

    public static String subString(String src, int length) {
        if (src == null) {
            return null;
        }
        int i = src.length();
        if (i > length) {
            return src.substring(0, length);
        }
        return src;
    }
}

Related

  1. HtmlEncode(String strInput)
  2. htmlEncode(String strSrc)
  3. htmlEncode(String strSrc)
  4. htmlEncode(String text)
  5. htmlEncode(String txt)
  6. htmlEncoded(String text)
  7. htmlEncoder(String content)
  8. htmlEncoding(String str)
  9. htmlEntityEncode(String s)