Split the given String into tokens. - Java java.lang

Java examples for java.lang:String Split

Description

Split the given String into tokens.

Demo Code

/*//from  ww w .j  ava  2 s.  c om
 * Static String formatting and query routines.
 * Copyright (C) 2001-2005 Stephen Ostermiller
 * http://ostermiller.org/contact.pl?regarding=Java+Utilities
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * See COPYING.TXT for details.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String s = "java2s.com";
        String delimiter = "java2s.com";
        System.out.println(java.util.Arrays.toString(split(s, delimiter)));
    }

    /**
     * Split the given String into tokens.
     * <P>
     * This method is meant to be similar to the split
     * function in other programming languages but it does
     * not use regular expressions.  Rather the String is
     * split on a single String literal.
     * <P>
     * Unlike java.util.StringTokenizer which accepts
     * multiple character tokens as delimiters, the delimiter
     * here is a single String literal.
     * <P>
     * Each null token is returned as an empty String.
     * Delimiters are never returned as tokens.
     * <P>
     * If there is no delimiter because it is either empty or
     * null, the only element in the result is the original String.
     * <P>
     * StringHelper.split("1-2-3", "-");<br>
     * result: {"1","2","3"}<br>
     * StringHelper.split("-1--2-", "-");<br>
     * result: {"","1","","2",""}<br>
     * StringHelper.split("123", "");<br>
     * result: {"123"}<br>
     * StringHelper.split("1-2---3----4", "--");<br>
     * result: {"1-2","-3","","4"}<br>
     *
     * @param s String to be split.
     * @param delimiter String literal on which to split.
     * @return an array of tokens.
     * @throws NullPointerException if s is null.
     *
     * @since ostermillerutils 1.00.00
     */
    public static String[] split(String s, String delimiter) {
        int delimiterLength;
        // the next statement has the side effect of throwing a null pointer
        // exception if s is null.
        int stringLength = s.length();
        if (delimiter == null
                || (delimiterLength = delimiter.length()) == 0) {
            // it is not inherently clear what to do if there is no delimiter
            // On one hand it would make sense to return each character because
            // the null String can be found between each pair of characters in
            // a String.  However, it can be found many times there and we don'
            // want to be returning multiple null tokens.
            // returning the whole String will be defined as the correct behavior
            // in this instance.
            return new String[] { s };
        }

        // a two pass solution is used because a one pass solution would
        // require the possible resizing and copying of memory structures
        // In the worst case it would have to be resized n times with each
        // resize having a O(n) copy leading to an O(n^2) algorithm.

        int count;
        int start;
        int end;

        // Scan s and count the tokens.
        count = 0;
        start = 0;
        while ((end = s.indexOf(delimiter, start)) != -1) {
            count++;
            start = end + delimiterLength;
        }
        count++;

        // allocate an array to return the tokens,
        // we now know how big it should be
        String[] result = new String[count];

        // Scan s again, but this time pick out the tokens
        count = 0;
        start = 0;
        while ((end = s.indexOf(delimiter, start)) != -1) {
            result[count] = (s.substring(start, end));
            count++;
            start = end + delimiterLength;
        }
        end = stringLength;
        result[count] = s.substring(start, end);

        return (result);
    }
}

Related Tutorials