Here you can find the source of split(String str, char separator)
Parameter | Description |
---|---|
str | a parameter |
separator | a parameter |
static public String[] split(String str, char separator)
//package com.java2s; /******************************************************************************* * Copyright (c) 2004, 2006 svnClientAdapter project and others. * * 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.//from w ww. j ava 2s .c om * * Contributors: * svnClientAdapter project committers - initial API and implementation ******************************************************************************/ import java.util.ArrayList; import java.util.List; public class Main { /** * we can't use String.split as it is a JDK 1.4 method. * //TODO Java 1.4 is not aproblem anymore. Isn't it ? * @param str * @param separator * @return an array of string segments */ static public String[] split(String str, char separator) { int pos = 0; List list = new ArrayList(); int length = str.length(); for (int i = 0; i < length; i++) { char ch = str.charAt(i); if (ch == separator) { list.add(str.substring(pos, i)); pos = i + 1; } } if (pos != length) { list.add(str.substring(pos, length)); } return (String[]) list.toArray(new String[list.size()]); } /** * split using a string separator * @param str * @param separator * @return an array of string segments */ static public String[] split(String str, String separator) { List list = new ArrayList(); StringBuffer sb = new StringBuffer(str); int pos; while ((pos = sb.indexOf(separator)) != -1) { list.add(sb.substring(0, pos)); sb.delete(0, pos + separator.length()); } if (sb.length() > 0) { list.add(sb.toString()); } return (String[]) list.toArray(new String[list.size()]); } }