1

I want to skip printing ", " in last iteration.

I want output like name, name, name

Output now i am getting is name, name, name,

StringBuffer stringBuffer = new StringBuffer();
for(MovieModel.Cast cast : movieModelList.get(position).getCastList()){
    stringBuffer.append(cast.getName() + ", ");
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
R.H
  • 318
  • 2
  • 12
  • 1
    Possible duplicate of [Clearest way to comma-delimit a list (Java)?](http://stackoverflow.com/questions/668952/clearest-way-to-comma-delimit-a-list-java) – Tom May 21 '17 at 18:41

5 Answers5

3

You can append the comma before you append the name. Like this:

StringBuffer stringBuffer = new StringBuffer();
for(MovieModel.Cast cast : movieModelList.get(position).getCastList()){
    if (stringBuffer.length() != 0) {
        stringBuffer.append(",");
    }
    stringBuffer.append(cast.getName());
}
amhev
  • 313
  • 1
  • 3
  • 12
2

It's easier to insert the delimiter first. But, append the delimiter from a variable:

StringBuffer stringBuffer = new StringBuffer();
String delim = "";
for(MovieModel.Cast cast : movieModelList.get(position).getCastList()){
  stringBuffer.append(delim);
  stringBuffer.append(cast.getName());
  delim = ",";
}

In this way, you don't append the , before the first element, but do before the subsequent elements.

A couple of notes:

  • Don't concatenate strings to append them to the buffer. This defeats the point of the buffer (somewhat, not entirely). Append one part, then append the other.
  • You almost certainly want to use a StringBuilder instead of StringBuffer:

    As of release JDK 5, [StringBuffer] has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
2

Why reinvent the wheel? Use StringUtils.join method from Apache Commons. Or you can inspire (copy+modify) from its sourcecode commons-lang-sources

// copied from https://commons.apache.org/proper/commons-lang/apidocs/src-html/org/apache/commons/lang3/StringUtils.html
public static String join(final Iterator<?> iterator, final String separator) {
    // handle null, zero and one elements before building a buffer
    if (iterator == null) {
        return null;
    }
    if (!iterator.hasNext()) {
        return EMPTY;
    }
    final Object first = iterator.next();
    if (!iterator.hasNext()) {
        return Objects.toString(first, "");
    }
    // two or more elements
    final StringBuilder buf = new StringBuilder(STRING_BUILDER_SIZE); // Java default is 16, probably too small
    if (first != null) {
        buf.append(first);
    }
    while (iterator.hasNext()) {
        if (separator != null) {
            buf.append(separator);
        }
        final Object obj = iterator.next();
        if (obj != null) {
            buf.append(obj);
        }
    }
    return buf.toString();
}
pufface
  • 263
  • 3
  • 11
  • I suggested to use lib OR inspire (copy) implementation from its codebase. In both cases you do not reinvent wheel. Answer was edited with extracted code and fixed broken link. – pufface Jul 23 '21 at 18:47
1

After the loop, just remove the trailing comma and space:

stringBuffer.delete(stringBuffer.length() - 2, stringBuffer.length());
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
0

Try

StringBuffer stringBuffer = new StringBuffer();
for(MovieModel.Cast cast : movieModelList.get(position).getCastList()){
   stringBuffer.append(cast.getName());
   stringBuffer.append(", ");     
}
stringBuffer.delete(stringBuffer.length() - 2, stringBuffer.length());
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53