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 getAttribute(String attribute, String text) {
        return getAttribute(attribute, text, 0);
    }

    public static String getAttribute(String attribute, String text, int idx) {
        int close = text.indexOf(">", idx);
        int attrIdx = text.indexOf(attribute + "=\"", idx);
        if (attrIdx == -1) {
            return null;
        }
        if (attrIdx > close) {
            return null;
        }
        int attrStartIdx = attrIdx + attribute.length() + 2;
        int attrCloseIdx = text.indexOf("\"", attrStartIdx);
        if (attrCloseIdx > close) {
            return null;
        }
        return unescapeXml(text.substring(attrStartIdx, attrCloseIdx));
    }

    public static String unescapeXml(String str) {
        str = str.replaceAll("&", "&");
        str = str.replaceAll("&lt;", "<");
        str = str.replaceAll("&gt;", ">");
        str = str.replaceAll("&quot;", "\"");
        str = str.replaceAll("&apos;", "'");
        return str;
    }
}