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.