Here you can find the source of splitByChar(final String message, final char ch)
Parameter | Description |
---|---|
message | the string to split |
ch | the char between which we split |
static List<String> splitByChar(final String message, final char ch)
//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; } }