Java Array Split splitWordsL(String s, boolean splitOnWs, char[] delimiters)

Here you can find the source of splitWordsL(String s, boolean splitOnWs, char[] delimiters)

Description

Split into words.

License

Open Source License

Parameter

Parameter Description
s String to split
splitOnWs whitespace causes split
delimiters array of other chars causing a split, must be sorted in ascending order, e.g. <code>{',','.'}</code>

Return

A LinkedList of Strings, the words s was split to

Declaration

public static final LinkedList splitWordsL(String s, boolean splitOnWs, char[] delimiters) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w .ja v a 2  s. c om*/
 * Copyright 2005--2008 Helsinki Institute for Information Technology
 *
 * This file is a part of Fuego middleware.  Fuego middleware is free
 * software; you can redistribute it and/or modify it under the terms
 * of the MIT license, included as the file MIT-LICENSE in the Fuego
 * middleware source distribution.  If you did not receive the MIT
 * license with the distribution, write to the Fuego Core project at
 * fuego-core-users@googlegroups.com.
 */

import java.util.Arrays;
import java.util.LinkedList;

public class Main {
    /** Split into words. 
     * 
     * @param s String to split
     * @param splitOnWs whitespace causes split
     * @param delimiters array of other chars causing a split, must be sorted
     *    in ascending order, e.g. <code>{',','.'}</code>
     * @return A LinkedList of Strings, the words <code>s</code> was split to
     */
    public static final LinkedList splitWordsL(String s, boolean splitOnWs, char[] delimiters) {
        if (s == null)
            return null;
        int cstart = -1, // -1 = scanning space, other = startpos of content
                len = s.length();
        //A! assert _splitWords_assert(delimiters);
        LinkedList l = new LinkedList();
        for (int pos = 0; pos < len; pos++) {
            char ch = s.charAt(pos);
            if (cstart < 0) {
                if (splitOnWs && (ch == 0x0a || Character.isSpaceChar(ch)))
                    continue;
                if (delimiters != null && Arrays.binarySearch(delimiters, ch) > -1)
                    continue;
                cstart = pos;
            } else {
                boolean endOfWord = false;
                if (splitOnWs && ((ch == 0x0a || Character.isSpaceChar(ch)))) {
                    endOfWord = true;
                }
                if (delimiters != null && Arrays.binarySearch(delimiters, ch) > -1) {
                    endOfWord = true;
                }
                if (!endOfWord) {
                    continue;
                }
                l.add(s.substring(cstart, pos));
                cstart = -1;
            }
        }
        if (cstart >= 0)
            l.add(s.substring(cstart)); // Emit final token
        return l;
    }
}

Related

  1. splitByteArray(byte[] inByteArray, int length)
  2. splitData(String[] sData, int numMaps)
  3. splitOrderByFirst(byte[] pattern, byte[] src, int offset)
  4. splitSentences(String[] sentence, int limitLength, String reg)
  5. splitStringOnFields(String in, int[] cuts)
  6. unsplit(String[] array, String seperator)