Java String Split by Separator split(final String s, final char separator)

Here you can find the source of split(final String s, final char separator)

Description

Splits a String at character separator and trims spaces if present.

License

Open Source License

Parameter

Parameter Description
s incoming string
separator separator character

Return

an array of tokens found

Declaration

public static String[] split(final String s, final char separator) 

Method Source Code

//package com.java2s;
/* Copyright 2002 Health Level Seven, Inc. All Rights Reserved.
 *
 * This software is the proprietary information of Health Level Seven, Inc.
 * Use is subject to license terms.// ww w . j a  v  a 2 s.com
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Splits a String at character <code>separator</code> and trims spaces
     * if present.
     *
     * @param s  incoming string
     * @param separator  separator character
     * @return  an array of tokens found
     */
    public static String[] split(final String s, final char separator) {
        if (s == null)
            return null;

        final List<String> result = new ArrayList<String>();
        int j = -1;
        for (int i = s.indexOf(separator); i != -1; j = i, i = s.indexOf(separator, i + 1)) {
            result.add(s.substring(j + 1, i).trim());
        }
        result.add(s.substring(j + 1).trim());

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

Related

  1. split(byte[] b, int separator)
  2. split(CharSequence seq, char separator)
  3. split(final String expression, final char separator)
  4. split(final String input, final String separator)
  5. split(final String iSource, final char iRecordSeparator, final char... iJumpCharacters)
  6. split(final String str, final char separator)
  7. split(final String str, final char separator)
  8. split(final String str, final char separatorChar)
  9. split(final String str, final char separatorChar)