Here you can find the source of splitString(String str, String delimiter)
Parameter | Description |
---|---|
str | the string to be split |
delimiter | the delimiter |
public static String[] splitString(String str, String delimiter)
//package com.java2s; /*//from w ww .j av a 2s . co m * Copyright [2012-2014] PayPal Software Foundation * * 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.*; public class Main { /** * Manual split function to avoid depending on guava. * * <p> * Some examples: "^"=>[, ]; ""=>[]; "a"=>[a]; "abc"=>[abc]; "a^"=>[a, ]; "^b"=>[, b]; * "^^b"=>[, , b] * * @param str * the string to be split * @param delimiter * the delimiter * @return split string array */ public static String[] splitString(String str, String delimiter) { if (str == null || str.length() == 0) { return new String[] { "" }; } List<String> categories = new ArrayList<String>(); int dLen = delimiter.length(); int begin = 0; for (int i = 0; i < str.length(); i++) { if (str.substring(i, Math.min(i + dLen, str.length())).equals(delimiter)) { categories.add(str.substring(begin, i)); begin = i + dLen; } if (i == str.length() - 1) { categories.add(str.substring(begin, str.length())); } } return categories.toArray(new String[0]); } }