get Value Map For Delimited String - Java java.lang

Java examples for java.lang:String Tokenize

Description

get Value Map For Delimited String

Demo Code

//package com.java2s;

import java.util.HashMap;
import java.util.Map;

import java.util.StringTokenizer;

public class Main {


    public static Map<String, String> getValueMapForDelimitedString(
            String delimitedString) {
        Map<String, String> result = new HashMap<String, String>();
        if (isEmpty(delimitedString)) {
            return result;
        }/* w ww.j  av a  2 s  .co  m*/

        String[] parts = delimitedString.split("\\|");
        for (String part : parts) {
            String[] paramPararts = part.split("\\:");
            if (paramPararts.length == 2) {
                result.put(paramPararts[0].trim().toLowerCase(),
                        paramPararts[1].trim());
            }
        }

        return result;
    }

    public static boolean isEmpty(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }

        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(str.charAt(i)) == false) {
                return false;
            }
        }

        return true;
    }

    public static String[] split(String str, String delim) {
        return split(str, delim, true, false);
    }

    public static String[] split(String str, String delim, boolean trim,
            boolean include) {
        StringTokenizer tokens = new StringTokenizer(str, delim, include);
        String[] result = new String[tokens.countTokens()];
        int i = 0;
        while (tokens.hasMoreTokens()) {
            String token = tokens.nextToken();
            if (trim) {
                token = token.trim();
            }
            result[i++] = token;
        }
        return result;
    }

    public static String toLowerCase(String str) {
        return str == null ? null : str.toLowerCase();
    }
}

Related Tutorials