Escape < > ( ) # & letter - Android java.lang

Android examples for java.lang:String Escape

Description

Escape < > ( ) # & letter

Demo Code

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Main{


    public static String getSpclStrCnvrNew(String srcString) {

        String rtnStr = null;//www.jav  a  2 s  . c om

        try {
            StringBuffer strTxt = new StringBuffer("");

            char chrBuff;
            int len = srcString.length();

            for (int i = 0; i < len; i++) {
                chrBuff = (char) srcString.charAt(i);

                switch (chrBuff) {
                case '<':
                    strTxt.append("&lt;");
                    break;
                case '>':
                    strTxt.append("&gt;");
                    break;
                case '(':
                    strTxt.append("&#40;");
                    break;
                case ')':
                    strTxt.append("&#41;");
                    break;
                case '#':
                    strTxt.append("&#35;");
                    break;
                case '&':
                    strTxt.append("&amp;");
                    break;
                default:
                    strTxt.append(chrBuff);
                }
            }
            rtnStr = strTxt.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtnStr;
    }

}

Related Tutorials