Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static String replaceEntities(String s) {
        StringBuilder sb = new StringBuilder(s);
        int len = s.length();
        int skip = 0;
        int iSkip = 0;
        for (int i = 0; i < len; i++) {
            iSkip = i + skip;
            char c = sb.charAt(iSkip);
            if (c == '&') {
                sb.replace(iSkip, iSkip + 1, "&amp;");
                skip += 4;
            } else if (c == '\'') {
                sb.replace(iSkip, iSkip + 1, "&apos;");
                skip += 5;
            } else if (c == '<') {
                sb.replace(iSkip, iSkip + 1, "&lt;");
                skip += 3;
            } else if (c == '>') {
                sb.replace(iSkip, iSkip + 1, "&gt;");
                skip += 3;
            } else if (c == '"') {
                sb.replace(iSkip, iSkip + 1, "&quot;");
                skip += 5;
            }
        }
        return sb.toString();
    }
}