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 normalize(final String s) {
        if (s == null) {
            return "";
        }

        final StringBuffer str = new StringBuffer();
        final int len = s.length();

        for (int i = 0; i < len; i++) {
            final char ch = s.charAt(i);

            switch (ch) {
            case '<': {
                str.append("&lt;");
                break;
            }
            case '>': {
                str.append("&gt;");
                break;
            }
            case '&': {
                str.append("&amp;");
                break;
            }
            case '"': {
                str.append("&quot;");
                break;
            }
            case '\n': {
                if (i > 0) {
                    str.charAt(str.length() - 1);
                    str.append("\n");
                } else {
                    str.append("\n");
                }
                break;
            }
            default: {
                str.append(ch);
            }
            }
        }

        return (str.toString());
    }
}