Here you can find the source of splitStr(String str, char delimiter, boolean trim)
Parameter | Description |
---|---|
str | a parameter |
delimiter | a parameter |
public static List<String> splitStr(String str, char delimiter, boolean trim)
//package com.java2s; /******************************************************************************* * Copyright (c) 2012 eBay Inc. 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:/* w ww.j a va2s.c o m*/ * eBay Inc. - initial API and implementation *******************************************************************************/ import java.util.ArrayList; import java.util.List; public class Main { public static List<String> splitStr(String str, char delimiter) { return splitStr(str, delimiter, false); } /** * Splits string by delimiter. If delimiter is the last character * of the string, empty substring after that will not be added * to the result. * * @param str * @param delimiter * @return */ public static List<String> splitStr(String str, char delimiter, boolean trim) { int startPos = 0; final List<String> result = new ArrayList<String>(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c == delimiter) { String subStr = str.substring(startPos, i); if (trim) { subStr = subStr.trim(); } result.add(subStr); startPos = i + 1; continue; } } if (startPos < str.length()) { String subStr = str.substring(startPos, str.length()); if (trim) { subStr = subStr.trim(); } result.add(subStr); } return result; } }