0

I want to get all values of a set interface in one go as a comma separated string.

For Example(Java Language):

Set<String> fruits=  new HashSet<String>();

fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");

If I print the set as fruits.toString then the output would be:

[Apple, Banana, Orange]

But my requirement is Apple, Banana, Orange without the square brackets.

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
B. S. Rawat
  • 1,874
  • 3
  • 22
  • 34
  • Homework? You can try using String's substring() on the result of toString(). A better way would be to iterate over the Set and concatenate each element of the list with a StringBuilder. – Jim Ferrans May 29 '09 at 02:36
  • When asking questions, please specify language & make it a part of the tag. – shahkalpesh May 29 '09 at 02:48
  • Can someone add a 'VB' tag to this? Or VBScript, whatever it might be. – ChristianLinnell May 29 '09 at 02:49
  • Cant be VB. Uses semicolon & uses small toString. Makes it look like Java but Java doesn't have Set statement AFAIK – shahkalpesh May 29 '09 at 02:51
  • It's probably just Java, and the Set type (http://java.sun.com/javase/6/docs/api/java/util/Set.html). – Matthew Flaschen May 29 '09 at 03:07
  • In any case, declaring a Set object called fruits and never using it indicates the code should at least be cleaned up a little. – geofftnz May 29 '09 at 03:15
  • Java has Sets it's an abstract class hence using a HashSet as the instance class. It's not C# as it doesn't have Sets (Damn them!) – Omar Kooheji May 29 '09 at 11:04
  • 1
    Possible duplicate of [What's the best way to build a string of delimited items in Java?](http://stackoverflow.com/questions/63150/whats-the-best-way-to-build-a-string-of-delimited-items-in-java) – Raedwald Feb 23 '16 at 10:07

4 Answers4

3

I'm assuming this is Java.

MartinodF's quick and dirty toString().substring approach will work, but what you're really looking for is a join method. If you do a lot of string manipulation, I'd suggest you take a look at the Apache Commons Lang library. It provides a lot of useful features that are missing from the Java standard library, including a StringUtils class that would let you do this:

Set fruits =  new HashSet();

fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");

String allFruits = StringUtils.join(fruits, ", ");
// allFruits is now "Apple, Banana, Orange"
Moss Collum
  • 3,542
  • 3
  • 25
  • 23
  • Not bad idea, but for above code I have to used commons-lang.jar file which is not acceptable in my case. – B. S. Rawat May 29 '09 at 05:01
  • Because it's homework and he should have to figure it out himself? – Omar Kooheji May 29 '09 at 11:02
  • I am a developer in my team therefore I can’t ask architecture team to use a new jar file for only these kinds of transformation. So finally I have made a generic method which will convert collection data to comma separated string, I asked this question because I thought some intelligent guys know some better and optimized solution. – B. S. Rawat May 29 '09 at 15:45
  • Here is developed code: private String collectionToCommaSeparatedString(Collection value){ Iterator it = value.iterator(); StringBuilder result = new StringBuilder(); while ( it.hasNext() == true ) { result.append( it.next()); if(it.hasNext()) result.append(","); } return result.toString() ; } – B. S. Rawat May 29 '09 at 15:46
1

Quick and dirty:

value.toString().substring(1, value.toString().length - 1);
MartinodF
  • 8,157
  • 2
  • 32
  • 28
1

Assuming C# 3.5

var fruits = new HashSet<string>();

fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Orange");

Console.WriteLine(string.Join(", ",fruits.ToArray()));
geofftnz
  • 9,954
  • 2
  • 42
  • 50
0

Use StringUtils.join from commons lang

Set fruits =  new HashSet();

fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");


System.out.println(StringUtils.join(fruits, ','));
A_M
  • 7,693
  • 6
  • 33
  • 37