Here you can find the source of stringToMap(String src)
Parameter | Description |
---|---|
src | String containing name-value pairs. |
public static HashMap stringToMap(String src)
//package com.java2s; //License from project: Open Source License import java.util.HashMap; public class Main { /**// w w w. j ava 2 s . c o m * Converts a name-value pair string into a Map object. * * @param src String containing name-value pairs. * @return resulting Map object; will be empty if the string was null or * empty. */ public static HashMap stringToMap(String src) { HashMap<String, String> dest = new HashMap<String, String>(); if (src == null) { return (dest); } String line, key, val; int equalsPos, newLinePos, startPos = 0, len = src.length(); while (startPos < len) { newLinePos = src.indexOf('\n', startPos); // if the last line does not end with a newline character, // assume an imaginary newline character at the end of the string if (newLinePos == -1) { newLinePos = len; } line = src.substring(startPos, newLinePos); equalsPos = line.indexOf('='); if (equalsPos != -1) { key = line.substring(0, equalsPos); val = line.substring(equalsPos + 1); } else { key = line; val = null; } dest.put(key, val); startPos = newLinePos + 1; } return (dest); } }