Issue
So I have following strings,
String a = "123, 541, 123"
String b = "527"
String c = "234, 876"
I would like to loop over these strings, split the string by "," and store it in a set of string so that I have such final output,
("123", "541", "527", "234", "876")
Any idea how I can achieve this?
I have tried splitting the string but that results in Set<String[]>
and not sure how to proceed with this since I am very new to this.
Solution
First, you need to separate strings in "a" and "c" variable. For that you can you can you split() method. You can try code below and adapt it in a way that fits your needs.
Set<String> strings = new HashSet<>();
String a = "123, 541, 123";
String b = "527";
String c = "234, 876";
private void addToSet(String stringNumbers) {
for(String str : Arrays.asList(stringNumbers.split(","))) {
strings.add(str);
}
}
Answered By - JustQuest
Answer Checked By - Terry (JavaFixing Volunteer)