/**
* Feel free to copy and use this method
* If you have any comments or better solutions, please paste on my blog:
* http://blog.yanmingyu.com or email me feedback.andrewyu@gmail.com
*
* Sometimes we need to parse a string with key-value pairs into
* a Properties object for later use.
* For example:
* s = "a=1, b=2, c=3";
* Instead of using "split" method, we can parse this easily
* with the Properties load method.
* A piece of testing source code is listed as the following:
*
* String s= "a=1, b=2, c=3";
* Properties properties = parseStringToProperties(s, ',');
* System.out.println(properties.getProperty("a"));
*/
public static Properties parseStringToProperties(String s, char delimiter)
{
Properties properties = new Properties();
try
{
s = s.replace(delimiter, '\n');
StringReader reader = new StringReader(s);
properties.load(reader);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
return properties;
}
No comments:
Post a Comment