简体   繁体   中英

In for each loop i want to skip “, ” in last iteration

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() + ", ");
}

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());
}

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();
}

在循环之后,只需删除结尾的逗号和空格:

stringBuffer.delete(stringBuffer.length() - 2, stringBuffer.length());

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.

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM