Dave's Brain

Browse - Programming Tips - How can I best parse a variable=value pair in Java?

Date: 2012apr16
Language: Java

Q.  How can I best parse a variable=value pair in Java?

A.  Use split() like this:

String line = "language=java"; // example input String []pair = line.split("="); if (pair.length != 0) { String var = pair[0]; String val = pair.length > 1 ? pair[1] : ""; System.out.println(var + " equals + val); }
This works for cookies, .ini files, etc Regular expressions are another way. This does *not* deal with an equals in the value.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.