Java String Split by Delimiter split(String str, char delimiter, boolean trim)

Here you can find the source of split(String str, char delimiter, boolean trim)

Description

split

License

Open Source License

Declaration

public static String[] split(String str, char delimiter, boolean trim) 

Method Source Code


//package com.java2s;
/*//from   www  .j ava 2  s . c o  m
 * Copyright (c) 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * 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:
 *     Nuxeo - initial API and implementation
 *
 * $Id: StringUtils.java 28482 2008-01-04 15:33:39Z sfermigier $
 */

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

public class Main {
    public static String[] split(String str, char delimiter, boolean trim) {
        int s = 0;
        int e = str.indexOf(delimiter, s);
        if (e == -1) {
            if (trim) {
                str = str.trim();
            }
            return new String[] { str };
        }
        List<String> ar = new ArrayList<String>();
        do {
            String segment = str.substring(s, e);
            if (trim) {
                segment = segment.trim();
            }
            ar.add(segment);
            s = e + 1;
            e = str.indexOf(delimiter, s);
        } while (e != -1);

        int len = str.length();
        if (s < len) {
            String segment = str.substring(s);
            if (trim) {
                segment = segment.trim();
            }
            ar.add(segment);
        } else {
            ar.add("");
        }

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

Related

  1. split(String str, char delimiter)
  2. split(String str, char delimiter)
  3. split(String str, char delimiter)
  4. split(String str, char delimiter)
  5. split(String str, char delimiter)
  6. split(String str, int delim, String trailing)
  7. split(String str, String delim)
  8. split(String str, String delim)
  9. split(String str, String delim)