Java String Split by Char splitString(String string, char ch)

Here you can find the source of splitString(String string, char ch)

Description

split String

License

Open Source License

Declaration

public static List<String> splitString(String string, char ch) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 EclipseSource 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:/*ww w  .  ja v  a  2 s.c om*/
 *    EclipseSource - initial API and implementation
 ******************************************************************************/

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

public class Main {
    public static List<String> splitString(String string, char ch) {
        return splitString(string, ch, false);
    }

    public static List<String> splitString(String string, char ch, boolean skipElementLastIfEmpty) {
        List<String> parts = new ArrayList<String>();
        int beginIndex = 0;
        int endIndex = string.indexOf(ch);
        while (endIndex != -1) {
            String nextPart = string.substring(beginIndex, endIndex);
            parts.add(nextPart);
            beginIndex = endIndex + 1;
            endIndex = string.indexOf(ch, beginIndex);
        }
        if (!skipElementLastIfEmpty || beginIndex < string.length()) {
            String lastPart = string.substring(beginIndex, string.length());
            parts.add(lastPart);
        }
        return parts;
    }
}

Related

  1. splitOnChar(String str, int ch)
  2. splitOnCharArray(String value, String splitOn)
  3. SplitSearchString(String search_string, char split_char, boolean clear_exact_match_char)
  4. splitString(final String s, final char c, final boolean trimBlanks)
  5. splitString(String str, char sep)
  6. splitStringToChars(String str)
  7. splitTrim(String str, String limitChar)
  8. splitWithEscape(String s, String splitChars)