How can i fetch String as a String[] from env.properties file?

Viewed 35

I have property in env.properties columns = "{"abc","def",gef},{"xyz","dhf" ,"rewf"}"

In java class.. i want String[] columns = ?

1 Answers

If your format is somewhat standardized you can use a parser for that, f.e. a JSON parser if your property is in JSON format.

If you have a custom data format (for whatever reason), you have to parse the string by yourself. In the case you provided you could remove all "{", "}", and " from the string and then split by ",".

This can look like this

String s  = "{\"abc\",\"def\",gef},{\"xyz\",\"dhf\" ,\"rewf\"}";

// remove all unwanted characters
s = s.replaceAll("\\{", "")
     .replaceAll("\\}", "")
     .replaceAll("\\\"", "")
     .replaceAll("\\s", "");

String[] result = s.split(",");

System.out.println(Arrays.toString(result));

This would print

[abc, def, gef, xyz, dhf, rewf]
Related