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 getQueryParameter(String url, String key) {

        final String encodedKey = key;
        final int length = url.length();

        int start = 0;
        start = url.indexOf("?") + 1;

        do {
            int nextAmpersand = url.indexOf('&', start);
            int end = nextAmpersand != -1 ? nextAmpersand : length;

            int separator = url.indexOf('=', start);
            if (separator > end || separator == -1) {
                separator = end;
            }

            if (separator - start == encodedKey.length()
                    && url.regionMatches(start, encodedKey, 0, encodedKey.length())) {
                if (separator == end) {
                    return "";
                } else {
                    return (url.substring(separator + 1, end));
                }
            }

            // Move start to end of name.
            if (nextAmpersand != -1) {
                start = nextAmpersand + 1;
            } else {
                break;
            }
        } while (true);

        return null;
    }
}