Here you can find the source of splitEachArrayElementAndCreateMap(String[] array, String delimiter, String removeCharacters)
String
s, and for each element removes any instances of removeCharacter
, and splits the element based on the delimiter
.
Parameter | Description |
---|---|
array | the array to process |
delimiter | to split each element using (typically the equals symbol) |
removeCharacters | one or more characters to remove from each element prior to attempting the split operation (typically the quotation mark symbol) or <code>null</code> if no removal should occur |
Map
representing the array contents, or null
if the array to process was null or empty
public static Map splitEachArrayElementAndCreateMap(String[] array, String delimiter, String removeCharacters)
//package com.java2s; //License from project: Apache License import java.util.HashMap; import java.util.Map; public class Main { /**/*from w ww . j a va2 s . com*/ * Takes an array of <code>String</code>s, and for each element removes any instances of * <code>removeCharacter</code>, and splits the element based on the <code>delimiter</code>. A <code>Map</code> is * then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the * value.<p>Will trim both the key and value before adding to the <code>Map</code>.</p> * * @param array the array to process * @param delimiter to split each element using (typically the equals symbol) * @param removeCharacters one or more characters to remove from each element prior to attempting the split * operation (typically the quotation mark symbol) or <code>null</code> if no removal should occur * @return a <code>Map</code> representing the array contents, or <code>null</code> if the array to process was * null or empty */ public static Map splitEachArrayElementAndCreateMap(String[] array, String delimiter, String removeCharacters) { if ((array == null) || (array.length == 0)) { return null; } Map map = new HashMap(); for (int i = 0; i < array.length; i++) { String postRemove; if (removeCharacters == null) { postRemove = array[i]; } else { postRemove = replace(array[i], removeCharacters, ""); } String[] splitThisArrayElement = split(postRemove, delimiter); if (splitThisArrayElement == null) { continue; } map.put(splitThisArrayElement[0].trim(), splitThisArrayElement[1].trim()); } return map; } /** *Replacealloccurencesofasubstringwithinastringwith *anotherstring. *@paraminStringStringtoexamine *@paramoldPatternStringtoreplace *@paramnewPatternStringtoinsert *@returnaStringwiththereplacements */ public static String replace(String inString, String oldPattern, String newPattern) { if (inString == null) { return null; } if (oldPattern == null || newPattern == null) { return inString; } StringBuilder sbuf = new StringBuilder(); int pos = 0;//ourpositionintheoldstring int index = inString.indexOf(oldPattern); int patLen = oldPattern.length(); while (index >= 0) { sbuf.append(inString.substring(pos, index)); sbuf.append(newPattern); pos = index + patLen; index = inString.indexOf(oldPattern, pos); } sbuf.append(inString.substring(pos)); return sbuf.toString(); } /** * Splits a <code>String</code> at the first instance of the delimiter.<p>Does not include the delimiter in * the response.</p> * * @param toSplit the string to split * @param delimiter to split the string up with * @return a two element array with index 0 being before the delimiter, and index 1 being after the delimiter * (neither element includes the delimiter) * @throws IllegalArgumentException if an argument was invalid */ public static String[] split(String toSplit, String delimiter) { if (delimiter.length() != 1) { throw new IllegalArgumentException("Delimiter can only be one character in length"); } int offset = toSplit.indexOf(delimiter); if (offset < 0) { return null; } String beforeDelimiter = toSplit.substring(0, offset); String afterDelimiter = toSplit.substring(offset + 1); return new String[] { beforeDelimiter, afterDelimiter }; } }