Java String Split splitString(String src, String target)

Here you can find the source of splitString(String src, String target)

Description

split String

License

Open Source License

Declaration

public static String[] splitString(String src, String target) 

Method Source Code

//package com.java2s;
// License & terms of use: http://www.unicode.org/copyright.html#License

import java.util.ArrayList;

public class Main {
    public static String[] splitString(String src, String target) {
        return src.split("\\Q" + target + "\\E");
    }/*from   ww  w  .  j  a va2s .co  m*/

    /**
     * Split a string into pieces based on the given divider character
     * @param s the string to split
     * @param divider the character on which to split.  Occurrences of
     * this character are not included in the output
     * @param output an array to receive the substrings between
     * instances of divider.  It must be large enough on entry to
     * accomodate all output.  Adjacent instances of the divider
     * character will place empty strings into output.  Before
     * returning, output is padded out with empty strings.
     */
    public static void split(String s, char divider, String[] output) {
        int last = 0;
        int current = 0;
        int i;
        for (i = 0; i < s.length(); ++i) {
            if (s.charAt(i) == divider) {
                output[current++] = s.substring(last, i);
                last = i + 1;
            }
        }
        output[current++] = s.substring(last, i);
        while (current < output.length) {
            output[current++] = "";
        }
    }

    /**
     * Split a string into pieces based on the given divider character
     * @param s the string to split
     * @param divider the character on which to split.  Occurrences of
     * this character are not included in the output
     * @return output an array to receive the substrings between
     * instances of divider. Adjacent instances of the divider
     * character will place empty strings into output.
     */
    public static String[] split(String s, char divider) {
        int last = 0;
        int i;
        ArrayList<String> output = new ArrayList<String>();
        for (i = 0; i < s.length(); ++i) {
            if (s.charAt(i) == divider) {
                output.add(s.substring(last, i));
                last = i + 1;
            }
        }
        output.add(s.substring(last, i));
        return output.toArray(new String[output.size()]);
    }
}

Related

  1. splitString(String bigString, String splitter)
  2. splitString(String hexString, int partSize)
  3. splitString(String s)
  4. splitString(String s)
  5. splitString(String s)
  6. splitString(String string)
  7. splitString(String string, int limit)
  8. splitStringBySeperator(final String str, final String seperator)
  9. splitStrings(String start, String end, int numSplitStrings, boolean oddLevel, List splits)