Java String Split splitString(String s)

Here you can find the source of splitString(String s)

Description

split String

License

Apache License

Declaration

public static String[] splitString(String s) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    public final static String CRLF = "" + (char) 13 + (char) 10;

    public static String[] splitString(String s) {
        if (s == null)
            return new String[0];

        ArrayList<String> ret = new ArrayList<String>();
        int pos = 0;
        while (true) {
            int end = findNext(s, CRLF, pos);
            if (end < 0) {
                ret.add(s.substring(pos));
                break;
            } else {
                int len = end - pos;
                if (len > 0) {
                    ret.add(s.substring(pos, end));
                }//  www .  j  av a 2 s.  c  o  m
                pos = end + 1;
            }
        }

        return ret.toArray(new String[ret.size()]);
    }

    public static String[] splitString(String s, char c) {
        if (s == null)
            return new String[0];

        ArrayList<String> ret = new ArrayList<String>();
        int pos = 0;
        while (true) {
            int end = s.indexOf(s, pos);
            if (end < 0) {
                ret.add(s.substring(pos));
                break;
            } else {
                int len = end - pos;
                if (len > 0) {
                    ret.add(s.substring(pos, end));
                }
                pos = end + 1;
            }
        }

        return ret.toArray(new String[ret.size()]);
    }

    public static int findNext(String s, String chars, int fromIndex) {
        int p = Integer.MAX_VALUE;
        for (int i = 0; i < chars.length(); i++) {
            char c = chars.charAt(i);
            int p2 = s.indexOf(c, fromIndex);
            if (p2 < 0)
                continue;
            if (p2 < p)
                p = p2;
        }
        if (p == Integer.MAX_VALUE)
            return -1;
        else
            return p;
    }
}

Related

  1. splitStr(String str, int toCount)
  2. splitString(String bigString, String splitter)
  3. splitString(String hexString, int partSize)
  4. splitString(String s)
  5. splitString(String s)
  6. splitString(String src, String target)
  7. splitString(String string)
  8. splitString(String string, int limit)
  9. splitStringBySeperator(final String str, final String seperator)