0

I get a string of unit ids from this:

 String units = FolderUtil.getInstance().getUnits(folderId);

which looks like this:

[1886bec8-334b-4be0-bcb6-3594c7e454b6, 4818dda2-e124-460d-b910-dc0386ceb138, e95cdfcb-14d6-4065-a69d-8f0213e1f8b4]

How can I separate each id so I can pass each value individually into another method?

Jason
  • 754
  • 3
  • 8
  • 22
  • 3
    Possible duplicate of [How to split a comma-separated string?](http://stackoverflow.com/questions/10631715/how-to-split-a-comma-separated-string) – Ellis Feb 26 '16 at 20:04

2 Answers2

3

Here's a working solution using substring instead of replace:

String s1 = "[1886bec8-334b-4be0-bcb6-3594c7e454b6, 4818dda2-e124-460d-b910-dc0386ceb138, e95cdfcb-14d6-4065-a69d-8f0213e1f8b4]";
String s2 = s1.substring(1,s1.length()-1);
String[] ss = s2.split(",");
for (String str : ss) {
        System.out.println(str.trim());
}

Although by looking at that string, it looks like a string representation of an array of strings. Hence, if you developed the getUnits method, I'd advise you to refactor it to return the correct type and not the result of an arrayOfUnits.toString().

Baderous
  • 1,069
  • 1
  • 11
  • 32
1

Try this:

String units = FolderUtil.getInstance().getUnits(folderId);
String[] unitParts = units.replace("[","").replace("]", "").split(", ")
TayTay
  • 6,882
  • 4
  • 44
  • 65