Java Properties Create splitArrayElementsIntoProperties(String[] array, String delimiter)

Here you can find the source of splitArrayElementsIntoProperties(String[] array, String delimiter)

Description

split Array Elements Into Properties

License

Open Source License

Parameter

Parameter Description
array a parameter
delimiter a parameter

Return

Properties

Declaration

public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**/*from  ww w .jav  a 2 s.  co m*/
     *
     * @param array
     * @param delimiter
     * @return  Properties
     */
    public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter) {
        return splitArrayElementsIntoProperties(array, delimiter, null);
    }

    /**
     *
     * @param array
     * @param delimiter
     * @param charsToDelete
     * @return Properties
     */
    public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter,
            String charsToDelete) {

        if (array == null || array.length == 0) {
            return null;
        }

        Properties result = new Properties();
        for (int i = 0; i < array.length; i++) {
            String element = array[i];
            if (charsToDelete != null) {
                element = deleteAny(array[i], charsToDelete);
            }
            String[] splittedElement = split(element, delimiter);
            if (splittedElement == null) {
                continue;
            }
            result.setProperty(splittedElement[0].trim(), splittedElement[1].trim());
        }
        return result;
    }

    /**
     *
     * @param inString
     * @param charsToDelete
     * @return String
     */
    public static String deleteAny(String inString, String charsToDelete) {
        if (inString == null || charsToDelete == null) {
            return inString;
        }
        StringBuffer out = new StringBuffer();
        for (int i = 0; i < inString.length(); i++) {
            char c = inString.charAt(i);
            if (charsToDelete.indexOf(c) == -1) {
                out.append(c);
            }
        }
        return out.toString();
    }

    public static String[] split(String splittee, String splitChar) {
        return split(splittee, splitChar, 0);
    }

    /**
     * String split method
     * @param splittee  input string
     * @param splitChar split text
     * @param limit split limit number
     * @return  String[]
     */
    public static String[] split(String splittee, String splitChar, int limit) {
        String taRetVal[];
        StringTokenizer toTokenizer;
        int tnTokenCnt;

        try {
            toTokenizer = new StringTokenizer(splittee, splitChar);
            tnTokenCnt = toTokenizer.countTokens();
            if (limit != 0 && tnTokenCnt > limit)
                tnTokenCnt = limit;
            taRetVal = new String[tnTokenCnt];

            for (int i = 0; i < tnTokenCnt; i++) {
                if (toTokenizer.hasMoreTokens()) {
                    taRetVal[i] = toTokenizer.nextToken();
                }
                if (limit != 0 && limit == (i + 1))
                    break;
            }
        } catch (Exception e) {
            taRetVal = new String[0];
        }
        return taRetVal;
    }
}

Related

  1. splitArrayElementsIntoProperties(String[] array, String delimiter)
  2. toDictionary(Map properties)
  3. toFileContent(Properties props)
  4. toMap(byte[] source)