Escape string to create HTML String - Android java.lang

Android examples for java.lang:String Escape

Description

Escape string to create HTML String

Demo Code

import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static String toHTMLString(String s) {
        StringBuffer stringbuffer = new StringBuffer();
        for (int i = 0; s != null && i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '\'')
                // stringbuffer.append("&#39;");
                stringbuffer.append(c);/*from w  w  w  .  j  av a 2s. com*/
            else if (c == '"')
                // stringbuffer.append("&#34;");
                stringbuffer.append(c);
            else if (c == '\n')
                stringbuffer.append("<BR>\n");
            else if (c == '\t')
                stringbuffer.append("&nbsp;&nbsp;&nbsp;&nbsp;");
            else if (c == '<')
                // stringbuffer.append("&lt;");
                stringbuffer.append(c);
            else if (c == '>')
                // stringbuffer.append("&gt;");
                stringbuffer.append(c);
            else if (c == '&')
                stringbuffer.append("&amp;");
            else
                stringbuffer.append(c);
        }

        return stringbuffer.toString();
    }

}

Related Tutorials