-2

I have an ArrayList and I want to output completely as a String with separated by comma.

My code is

ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");

String listString = "";

for (String s : list)
{
    listString += s + ",";
}

System.out.println(listString);

But the output is one,two,three, and i want one,two,three without using replace method.

I am using JAVA

user3441151
  • 1,880
  • 6
  • 35
  • 79
  • 2
    `listString += (listString.isEmpty() ? "" : ",") + s;`. Note that you should use a `StringBuilder` rather than direct concatenation. – Andy Turner May 04 '16 at 11:58
  • 1
    @AndyTurner how to use StringBuilder for same? – user3441151 May 04 '16 at 12:01
  • String class has a method called join, so that you don't have to iterate through the collection to join the elements. [Here](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#join-java.lang.CharSequence-java.lang.Iterable-) is the doc – Rahul Sharma May 04 '16 at 12:05

7 Answers7

5

Using Java 8:

List<String> list = Arrays.asList(array);
String joinedString = String.join(",", list);
Krishnanunni P V
  • 689
  • 5
  • 18
  • 32
1

In Java 8+ you could use a StringJoiner (and you can use Arrays.asList(T...) to initialize your List). Something like,

List<String> list = Arrays.asList("one", "two", "three");
StringJoiner sj = new StringJoiner(",");
for (String s : list) {
    sj.add(s);
}
System.out.println(sj.toString());

Output is (as requested)

one,two,three
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0
import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        ArrayList<String> list = new ArrayList<String>();
        list.add("one");
        list.add("two");
        list.add("three");

        String listString = "";

        for (int i=0;i<list.size()-1;i++)
            listString += list.get(i) + ",";

        System.out.println(listString+list.get(list.size()-1));
    }
}

Output:

one,two,three

http://ideone.com/PkAOOq

riteshtch
  • 8,629
  • 4
  • 25
  • 38
0

Just use String.substring. Here you go:

listString = listString.substring(0, listString.length() - 1);
Adnan Isajbegovic
  • 2,227
  • 17
  • 27
0

You can use java.util.StringJoiner for this, instead of a List:

    StringJoiner joiner = new StringJoiner(",");
    joiner.add("one");
    joiner.add("two"),
    joiner.add("three");

    String listString = joiner.toString();

If you are not using Java8, then consider using StringBuilder. Then your final string can be:

System.out.println(listString.substring(0, listString.length() - 1));
ernest_k
  • 44,416
  • 5
  • 53
  • 99
0

For a pre Java 8 solution, since the default toString() gives you [one, two, three].

String s = list.toString();
s = s.substring(1, s.length()-1);
Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

try

list.toString().replace("[", "").replace("]", "");
Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33