Java String Unescape unescapeText(String s)

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

Description

Replaces &-excape sequences required by HTML and XML with the character represented by the escape sequence (& < > " and ').

License

LGPL

Parameter

Parameter Description
String the String to be unescaped

Return

a string witht the escaped characters replaced with the above characters

Declaration

public static String unescapeText(String s) 

Method Source Code

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

public class Main {
    /**//from w w w  .  j a  va  2s. c  o m
     * Replaces &-excape sequences required by HTML and XML with the character represented by the
     * escape sequence (& < > " and ').
     * @param String the String to be unescaped
     * @return a string witht the escaped characters replaced with the above characters
     */
    public static String unescapeText(String s) {
        StringBuffer sb = new StringBuffer();
        int oldIndex = 0;
        int index = s.indexOf('&');

        while (index >= 0) {
            sb.append(s.substring(oldIndex, index));

            if (s.startsWith("&amp;", index)) {
                sb.append('&');
                oldIndex = index + 5;
            } else if (s.startsWith("&lt;", index)) {
                sb.append('<');
                oldIndex = index + 4;
            } else if (s.startsWith("&gt;", index)) {
                sb.append('>');
                oldIndex = index + 4;
            } else if (s.startsWith("&quote;", index)) {
                sb.append('"');
                oldIndex = index + 7;
            } else if (s.startsWith("&apos;", index)) {
                sb.append('\'');
                oldIndex = index + 6;
            } else {
                sb.append('&');
                oldIndex = index + 1;
            }
            index = s.indexOf("&", oldIndex);
        }
        sb.append(s.substring(oldIndex));
        return sb.toString();
    }
}

Related

  1. unescapeString(String text)
  2. unescapeString(String text)
  3. unescapeString(String text)
  4. unescapeString(String txt)
  5. unescapeText(String s)
  6. unescapeText(String s)
  7. unescapeText(String s)