Arraylist of strings into one comma separated string

how to add comma separated values in string in java
convert integer list to comma separated string java 8
arraylist to string
java list to comma separated string with quotes
convert list of enum to comma separated string
how to convert array into comma separated string
convert comma separated string to map java 8
apache comma separated list

Trying to convert a Arraylist of strings into one big comma separated string.

However when I use the

String joined = TextUtils.join(", ", participants);

Debugger shows me size of 4 for participants however the joined value as "" therefore empty

private ArrayList<String> participants;

Not sure what is going wrong?

UPDATE:

List<String> list = new ArrayList<>();
list.add("Philip");
list.add("Paul Smith");
list.add("Raja");
list.add("Ez");

String s = TextUtils.join(", ", list);

This works when I have a list that I manually populate however below is how the code is working right now.

In the onCreate()

callApi(type);
String s = TextUtils.join(", ", participants);
getSupportActionBar().setTitle(s);

In callAPI():

JSONArray participantsR = sub.getJSONArray("referralParticipants");

Log.e("Participants length ", String.valueOf(participantsR.length()));

for (int i = 0; i < participantsR.length(); i++)
{
    JSONObject object = participantsR.getJSONObject(i);
    String firstname = (String) object.get("fullName");
    participants.add(firstname);
    Log.e("Times", String.valueOf(i));
}

I'm trying to reproduce your error and am unable to. Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_temp);

    List<String> list = new ArrayList<>();
    list.add("Philip Johnson");
    list.add("Paul Smith");
    list.add("Raja P");
    list.add("Ezhu Malai");

    String s = TextUtils.join(", ", list);

    Log.d(LOGTAG, s);
}

My output is Philip Johnson, Paul Smith, Raja P, Ezhu Malai as expected.

Are you importing the correct TextUtils class?

android.text.TextUtils;

Given the new information, here is my approach:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_temp);

    callApi(type, new OnResponseListener<List<String>>() {
        @Override public void onResponse(List<String> list) {
            getSupportActionBar().setTitle(TextUtils.join(", ", list));
        }
    });
}

I don't know what networking library you're using, but you may have to define OnResponseListener as an interface. It's very easy:

public interface OnResponseListener<T> {
    public void onResponse(T response);
}

You will then need to modify your callApi function to take an instance of OnResponseListener> and call it's onResponse method after completing the call.

I would recommend looking into the Volley library, and reading the Android documentation about simple network calls.

Arraylist of strings into one comma separated string, With Java 7-, there is a dirty way (note: it works only if you don't insert strings which Joins the elements of the provided array/iterable into a single String I am having ArrayList of String, which I need to convert to comma separated list,  Trying to convert a Arraylist of strings into one big comma separated string. However when I use the String joined = TextUtils.join(", ", participants); Debugger shows me size of 4 for partici

I use StringUtils.join from Apache Common Utilities.

The code is super-simple just the way you wanted,

StringUtils.join(participants,", ");

Works flawlessly for me.

EDIT

As requested, here is the StringUtils.java file for those who just want to use this single utility class and not the entire library.

How to convert a List<String> into a comma separated string without , Let's have a look on how to do that. Java 8 (or above). We simply can write String. join(..), pass a delimiter and an Iterable, then the  Converting a List<String> to a String with all the values of the List comma separated in Java 8 is really straightforward. Let’s have a look how to do that.

I don't know what TextUtils does. This will do it.

StringBuffer sb = new StringBuffer();
for (String x : participants) {
    sb.append(x);
    sb.append(", ");
}
return sb.toString();

Easy enough, just use that.

Java 8, Let's have a look how to do that. In Java 8. We can simply write String.join(..), pass a delimiter and an Iterable and the new StringJoiner will do  Convert a List<String> in a String with all the values of the List comma separated using Java 8 is really straightforward. Let’s have a look on how to do that. Java 8 (or above) We simply can write String.join(..), pass a delimiter and an Iterable, then the new StringJoiner will do the rest:

Try with kotlin

val commaSeperatedString = listOfStringColumn.joinToString { it -> 
 "\'${it.nameOfStringVariable}\'" }

// output: 'One', 'Two', 'Three', 'Four', 'Five'

Convert a List to a Comma-Separated String in Java 8, Given a List of String, the task is to convert the List to a comma separated String in Java. Form a comma separated String from the List of String using join() method by passing comma ', ' and list = new ArrayList<>( If you like GeeksforGeeks and would like to contribute, you can also write an article using Java-Strings. Convert comma separated String to List List<String> items = Arrays.asList (str.split ("\s*,\s*")); The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace which will place the words into the list and collapse any whitespace between the words and commas.

Convert a List of String to a comma separated String in Java , asList() method together to create an ArrayList from any delimited String, not just comma separated one. All you need to do is first split the given  How to parse strings using String.Split in C#. 01/03/2018; 2 minutes to read +3; In this article. The String.Split method creates an array of substrings by splitting the input string based on one or more delimiters. This method is often the easiest way to separate a string on word boundaries.

How to Convert a Comma Separated String to an ArrayList in Java , To convert a comma separated String into an ArrayListSplit the String into an array of Strings using the split() method.Now, convert the obtained  Get the ArrayList of Strings. Find the size of ArrayList using size() method, and Create a String Array of this size. Fetch each element of the ArrayList one by one using get() method. Assigned each element into String Array using assignment(=) operator. Print String Array. Below is the implementation of the above approach:

How to convert a comma separated String into an ArrayList in Java?, Related Articles. Java - Convert comma-separated String to a List · How to read file in Java - BufferedReader · Java - Convert String to  Call the string.Join method to combine an array of strings into one string with a separator.

Comments
  • I just copied it and it worked your way. However I am adding the array from a API call. When I revert back to replacing the list with the list updated from the API then it goes back to not showing anything in the join object. Even though the list has 4 string elements.
  • I have updated my question with more code on how my list is being populated
  • You should post your network call code. I'm assuming that what is happening is that you aren't waiting on the completion of the call before doing the join, and are therefore joining on an empty list. It won't look like that when debugging, because the networking thread will continue and will populate the list while you're opening the debugging tools.
  • Thanks I forgot about that, I have a handler and I put the code in there and now it all works!
  • I would second this answer, but Android has a Dex limit, and Apache Commons is a massive library.
  • Just use the single StringUtils class. You don't need to include the entire library.
  • For my own sake, I don't know how to import only a single class from a library. Would you mind pointing to an example of how it can be done?
  • Have updated the question with the single class file only. Just copy paste this class into your project and you are good to go. Do upvote if it helps.
  • Its giving sb as "". participants still has 4 objects of type string in it.