Here you can find the source of splitSimple(String delimiter, String str)
Parameter | Description |
---|---|
delimiter | The delimiter to split the string using |
str | The string to split |
public static List<String> splitSimple(String delimiter, String str)
//package com.java2s; /*/*from w ww .j a va2 s . com*/ * Copyright (C) 2013 salesforce.com, inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ArrayList; import java.util.List; public class Main { /** * Splits the given string str using the given delimiter and returns the result as a string list. If str is null, * then null is returned.<br> * <br> * The returned string list is an ArrayList that is constructed using the 4 as the ArrayList's initial size. If you * expect to have more than four elements more than just on the rare occasion, then please consider using another * splitSimple overload that lets you pass in the expected size.<br> * <br> * This is more efficient than String.split or TextUtil.split because it does not use a regular expression.<br> * <br> * <b>CAUTION:</b> The str and delimiter parameters are in an order that differs from other string splitting * methods. Be absolutely sure that you get the str and delimiter parameter arguments correct. This may eventually * be fixed with a refactoring. * * @param delimiter The delimiter to split the string using * @param str The string to split * @return String list or, if str was null, then null */ public static List<String> splitSimple(String delimiter, String str) { return splitSimple(delimiter, str, 4); } /** * Splits the given string str using the given delimiter and returns the result as a string list. If str is null, * then null is returned.<br> * <br> * The returned string list is an ArrayList that is constructed using the given expected size as the ArrayList's * initial size. If you are not aware of the expected size, then use 0, which will cause this method to use a * LinkedList instead of an ArrayList.<br> * <br> * This is more efficient than String.split or TextUtil.split because it does not use a regular expression.<br> * <br> * <b>CAUTION:</b> The str and delimiter parameters are in an order that differs from other string splitting * methods. Be absolutely sure that you get the str and delimiter parameter arguments correct. This may eventually * be fixed with a refactoring. * * @param delimiter The delimiter to split the string using * @param str The string to split * @param expectedSize The expected number of elements in the output list. If you don't know, or if it could be * arbitrarily large, and if you will only access the returned list sequentially with an iterator, then * use 0 to tell this method to use a LinkedList * @return String list or, if str was null, then null */ public static List<String> splitSimple(String delimiter, String str, int expectedSize) { return splitSimple(str, delimiter, expectedSize, false); } private static List<String> splitSimple(String s, String split, int expectedSize, boolean shouldTrim) { return splitSimple(s, split, expectedSize, shouldTrim, false); } private static List<String> splitSimple(String str, String delimiter, int expectedSize, boolean shouldTrim, boolean ignoreTrailingEmpty) { if (str == null) { return null; } List<String> result = (expectedSize == 0) ? new ArrayList<>() : new ArrayList<>(expectedSize); if (delimiter.length() == 0) { if (!ignoreTrailingEmpty) { throw new IllegalArgumentException(); } // Special case to match java's behavior char[] chars = new char[str.length()]; str.getChars(0, str.length(), chars, 0); result.add(""); for (char c : chars) { result.add(Character.toString(c)); } return result; } // Special case to match java's behavior if (ignoreTrailingEmpty && "".equals(str)) { result.add(""); return result; } int start = 0; int indexof; while ((indexof = str.indexOf(delimiter, start)) != -1) { String substring = str.substring(start, indexof); if (shouldTrim) { substring = substring.trim(); } result.add(substring); start = indexof + delimiter.length(); if (start >= str.length()) { break; } } if (start == str.length()) { result.add(""); } else if (start < str.length()) { String substring = str.substring(start); if (shouldTrim) { substring = substring.trim(); } result.add(substring); } if (ignoreTrailingEmpty && result.size() > 0) { // Discard empty substrings at the end for (int i = result.size() - 1; i >= 0; i--) { if (result.get(i).equals("")) { result.remove(i); } else { break; } } } return result; } }