join Iterable<T> to string - Android java.lang

Android examples for java.lang:String Join

Description

join Iterable<T> to string

Demo Code

import android.content.Context;

public class Main{

    public static <T> String join(Iterable<T> list, String separator) {
        StringBuilder sb = new StringBuilder();
        String tempSeparator = "";
        for (T o : list) {
            sb.append(tempSeparator).append(o);
            tempSeparator = separator;/*from   ww w .  ja v a2 s.co  m*/
        }
        return sb.toString();
    }
    public static <T> String join(Iterable<T> list, String separator,
            int limitLen) {
        StringBuilder sb = new StringBuilder();
        String tempSeparator = "";
        for (T o : list) {
            if (sb.length() + tempSeparator.length()
                    + o.toString().length() + 3 >= limitLen) {
                sb.append("...");
                break;
            }
            sb.append(tempSeparator).append(o);
            tempSeparator = separator;
        }
        return sb.toString();
    }

}

Related Tutorials