Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Usually you will want to parse the string into a map because you will be pulling various values perhaps multiple times, so it often makes sense to pay the parsing cost up-front.</p> <p>If not, then here is how I would solve the problem (assuming you want to differentiate between int values and String values).:</p> <pre><code>public Object pullValue(String pairs, String key) { boolean returnString = false; int keyStart = pairs.indexOf(key + "="); if (keyStart &lt; 0) { logger.error("Key " + key + " not found in key-value pairs string"); return null; } int valueStart = keyStart + key.length() + 1; if (pairs.charAt(valueStart) == '"') { returnString = true; valueStart++; // Skip past the quote mark } int valueEnd; if (returnString) { valueEnd = pairs.indexOf('"', valueStart); if (valueEnd &lt; 0) { logger.error("Unmatched double quote mark extracting value for key " + key) } return pairs.substring(valueStart, valueEnd); } else { valueEnd = pairs.indexOf(',', valueStart); if (valueEnd &lt; 0) { // If this is the last key value pair in string valueEnd = pairs.length(); } return Integer.decode(pairs.substring(valueStart, valueEnd)); } } </code></pre> <p>Note that this solution assumes no spaces between the key, the equals sign, and the value. If these are possible you will have to create some code to travel the string between them.</p> <p>Another solution is to use a regular expression parser. You could do something like (this is untested):</p> <pre><code>Pattern lookingForString = Pattern.compile(key + "[ \t]*=[ \t]*[\"]([^\"]+)[\"]"); Pattern lookingForInt = Pattern.compile(key + "[ \t]*=[ \t]*([^,]+)"); Matcher stringFinder = lookingForString.matcher(pairs); Matcher intFinder = lookingForInt.matcher(pairs); if (stringFinder.find()) { return stringFinder.group(1); } else if (intFinder.find()) { return Integer.decode(intFinder.group(1)); } else { logger.error("Could not extract value for key " + key); return null; } </code></pre> <p>HTH</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload