Java String Split by Space splitOnSpace(String string)

Here you can find the source of splitOnSpace(String string)

Description

split On Space

License

Open Source License

Declaration

public static final String[] splitOnSpace(String string) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005, 2007 Remy Suen/*  www.  j a v  a2 s  .c om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Remy Suen <remy.suen@gmail.com> - initial API and implementation
 ******************************************************************************/

import java.util.ArrayList;

public class Main {
    public static final String[] splitOnSpace(String string) {
        int index = string.indexOf(' ');
        if (index == -1) {
            return new String[] { string };
        }

        ArrayList split = new ArrayList();
        while (index != -1) {
            split.add(string.substring(0, index));
            string = string.substring(index + 1);
            index = string.indexOf(' ');
        }

        if (!string.equals("")) { //$NON-NLS-1$
            split.add(string);
        }

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

Related

  1. splitAtSpaces(String s)
  2. splitBySpace(String p_str)
  3. splitInWhiteSpaces(String string)
  4. splitNamespaceTitle(String fullTitle)
  5. splitOnSpace(final String string)
  6. SplitOnWhitespace(String instrData)
  7. splitSpaces(String input)
  8. splitStringOnWhitespace(String text)
  9. splitWithoutWhitespace(String source, String separator)