Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * XML encode a string.
     * @param str the string to encode.
     * @return the encoded string.
     */
    public static String escapeContent(String str) {
        final StringBuilder b = new StringBuilder();
        final char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; ++i) {
            final char c = chars[i];
            switch (c) {
            case '<':
                b.append("&lt;");
                break;
            case '>':
                b.append("&gt;");
                break;
            case '&':
                b.append("&amp;");
                break;
            default:
                b.append(c);
            }
        }
        return b.toString();
    }
}