Java String Split by Char splitByChar(final String message, final char ch)

Here you can find the source of splitByChar(final String message, final char ch)

Description

split string at each occurrence of a character (e.g.

License

Open Source License

Parameter

Parameter Description
message the string to split
ch the char between which we split

Return

the list lines

Declaration

static List<String> splitByChar(final String message, final char ch) 

Method Source Code

//package com.java2s;
/*/*from  www  . ja va 2s. co  m*/
 *  Copyright (c) 2011-2015 The original author or authors
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *       The Eclipse Public License is available at
 *       http://www.eclipse.org/legal/epl-v10.html
 *
 *       The Apache License v2.0 is available at
 *       http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * split string at each occurrence of a character (e.g. \n)
     *
     * @param message the string to split
     * @param ch      the char between which we split
     * @return the list lines
     */
    static List<String> splitByChar(final String message, final char ch) {
        List<String> lines = new ArrayList<>();
        int index = 0;
        int nextIndex;
        while ((nextIndex = message.indexOf(ch, index)) != -1) {
            lines.add(message.substring(index, nextIndex));
            index = nextIndex + 1;
        }
        lines.add(message.substring(index));
        return lines;
    }
}

Related

  1. split(String toSplit, char splitChar, boolean trim)
  2. split(String val, char ch)
  3. split(String value, char splitChar)
  4. splitAt(String inputString, Character inputChar)
  5. splitAt(String str, char c)
  6. splitByNonNestedChars(String s, char... c)
  7. splitChars(String input, String charsToBeRemoved)
  8. splitEncolosed(String s, char open_tag, char close_tag)
  9. splitFast3(String data, char splitChar)