Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.text.TextUtils;

public class Main {
    public static List<String> split(String str, char delimiter) {

        if (TextUtils.isEmpty(str)) {
            return Collections.emptyList();
        }

        int substringsCount = 1;
        for (int i = 0, len = str.length(); i < len; i++) {
            if (str.charAt(i) == delimiter) {
                substringsCount++;
            }
        }

        if (substringsCount == 1) {
            return Collections.singletonList(str);
        }

        List<String> split = new ArrayList<String>(substringsCount);

        int index = str.indexOf(delimiter);
        int lastIndex = 0;
        while (index != -1) {

            split.add(str.substring(lastIndex, index));

            lastIndex = index + 1;
            index = str.indexOf(delimiter, lastIndex);
        }

        split.add(str.substring(lastIndex, str.length())); // add the final string after the last delimiter

        return split;
    }
}