extract Value From Query String And Key - Java java.net

Java examples for java.net:URL Query

Description

extract Value From Query String And Key

Demo Code

//package com.java2s;

import java.util.StringTokenizer;

public class Main {
    public static void main(String[] argv) {
        String key = "abc";
        String queryString = "&abc=java2s.com";
        System.out.println(extractValueFromQueryStringAndKey(key,
                queryString));/* w w w  .  ja  v  a2s  . com*/
    }

    public static String extractValueFromQueryStringAndKey(String key,
            String queryString) {
        String foundValue = null;
        StringTokenizer queryItems = new StringTokenizer(queryString, "&");
        while (queryItems.hasMoreTokens() && foundValue == null) {
            String queryParameter = queryItems.nextToken();
            StringTokenizer paramTokenizer = new StringTokenizer(
                    queryParameter, "=");
            String currentKey = paramTokenizer.nextToken();
            String currentValue = paramTokenizer.nextToken();
            if (currentKey.equalsIgnoreCase(key)) {
                foundValue = currentValue;
            }
        }
        return foundValue;
    }
}

Related Tutorials