Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Escape the "special" characters as required for inclusion in XML elements
     * Replaces all incidences of & with & < with < > with > " with " ' with
     * &apos;
     * 
     * @param s
     *            The string to scan
     * @return String
     */
    public static String escape(String s) {
        char[] array = s.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            switch (array[i]) {
            case '&':
                sb.append("&amp;");
                break;
            case '<':
                sb.append("&lt;");
                break;
            case '>':
                sb.append("&gt;");
                break;
            case '"':
                sb.append("&quot;");
                break;
            case '\'':
                sb.append("&apos;");
                break;
            default:
                sb.append(array[i]);
            }
        }
        return sb.toString();
    }
}