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

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

Description

split

License

Open Source License

Declaration

public static List<String> split(String s, char separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 Oliver Br?samle/*from w ww  . j a  v a2s .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:
 *    Oliver Br?samle - initial API and implementation and/or initial documentation
 *    Andrey Loskutov <loskutov@gmx.de> - review, cleanup and bugfixes
 *******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<String> split(String s, char separator) {
        int i = s.indexOf(separator);
        List<String> list = new ArrayList<>();
        if (i < 0) {
            list.add(s);
            return list;
        }
        for (; i >= 0 && i < s.length();) {
            String sub = s.substring(0, i).trim();
            if (!sub.isEmpty()) {
                list.add(sub);
            }
            s = s.substring(i + 1).trim();
            i = s.indexOf(separator);
            if (i < 0) {
                if (!s.isEmpty()) {
                    list.add(s);
                }
                break;
            }
        }
        return list;
    }
}

Related

  1. split(final T string, final char separator)
  2. split(String array, String separators)
  3. split(String input, int separator)
  4. split(String left, String separator)
  5. split(String s, char separator)
  6. split(String s, char separator)
  7. split(String s, char separator)
  8. split(String s, char separator)
  9. split(String s, char separator)