miercuri, 6 mai 2015

[OmniFaces utilities 2.0] Convert comma (or specified delimiter) separated values in a string into a list of strings


[OmniFaces utilities] The csvToList() method converts comma (or specified delimiter) separated values in a string into a list with those values.

Method:
See also: Utils#isEmpty()
Usage:

using the default delimiter, ","

String clientIds = "{\"cid\":\"formId_Ajax:name\"},{\"cid\":\"formId_Ajax:surname\"},{\"cid\":\"formId_Ajax:city\"},{\"cid\":\"formId_Ajax:state\"},{\"cid\":\"formId_Ajax:zipcode\"},{\"cid\":\"formId_Ajax:address\"},{\"cid\":\"formId_Ajax:email\"}";

List<String> listClientIds = Utils.csvToList(clientIds);
for (String clientId : listClientIds) {
     System.out.println(clientId);
}

specify the delimiter, (e.g. ";")

String clientIds = "{\"cid\":\"formId_Ajax:name\"};{\"cid\":\"formId_Ajax:surname\"};{\"cid\":\"formId_Ajax:city\"};{\"cid\":\"formId_Ajax:state\"};{\"cid\":\"formId_Ajax:zipcode\"};{\"cid\":\"formId_Ajax:address\"};{\"cid\":\"formId_Ajax:email\"}";

List<String> listClientIds = Utils.csvToList(clientIds, ";");
for (String clientId : listClientIds) {
     System.out.println(clientId);
}

Output in both use cases:

{"cid":"formId_Ajax:name"}
{"cid":"formId_Ajax:surname"}
{"cid":"formId_Ajax:city"}
{"cid":"formId_Ajax:state"}
{"cid":"formId_Ajax:zipcode"}
{"cid":"formId_Ajax:address"}
{"cid":"formId_Ajax:email"}

Niciun comentariu:

Trimiteți un comentariu