Split string to list which includes elements with numPreRow length - Android java.lang

Android examples for java.lang:String Split

Description

Split string to list which includes elements with numPreRow length

Demo Code

import java.io.UnsupportedEncodingException;
import java.util.*;

public class Main{

    /**/*from  w  w w. ja  va  2s  .com*/
     * Split <code>sb</code> to <code>numPreRow</code>
     *
     * @param sb        string buffer to be splited
     * @param numPreRow row number
     * @return <code>ArrayList</code>
     */
    public static List<String> splitContent(StringBuffer sb, int numPreRow) {
        return splitContent(sb.toString(), numPreRow);
    }
    /**
     * Split string to list which includes elements with numPreRow length
     *
     * @param src       source string
     * @param numPreRow row number
     * @return <code>ArrayList</code>
     */
    public static List<String> splitContent(String src, int numPreRow) {
        List<String> result = new ArrayList<String>();
        if (src == null || "".equals(src)) {
            return null;
        }
        if (numPreRow == 0) {
            result.add(src);
            return result;
        }
        for (int i = 0; i < src.length(); i++) {
            if ((i + 1) * numPreRow <= src.length()) {
                String temStr = src.substring(i * numPreRow, i * numPreRow
                        + numPreRow);
                result.add(temStr);
            } else if (i * numPreRow <= src.length()
                    && (i + 1) * numPreRow > src.length()) {
                result.add(src.substring(i * numPreRow, src.length()));
            }
        }
        return result;
    }

}

Related Tutorials