Here you can find the source of splitChars(String str, String delimiters)
public static String[] splitChars(String str, String delimiters)
//package com.java2s; /**//from www . j av a2s . c o m * Copyright Sangram Jadhav. All rights reserved. * * 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 { public static String[] splitChars(String str, String delimiters) { if (str == null) return null; List<String> results = new ArrayList<String>(); int lastpos = 0; for (int i = 0, len = str.length(); i < len; i++) { char c = str.charAt(i); if (delimiters.indexOf(c) != -1) { results.add(str.substring(lastpos, i)); lastpos = i + 1; } } results.add(str.substring(lastpos)); return results.toArray(new String[results.size()]); } /** * Finds first occurrence of a substring in the given source but within limited range [start, end). * It is fastest possible code, but still original <code>String.indexOf(String, int)</code> * is much faster (since it uses char[] value directly) and should be used when no range is needed. * * @param s source string for examination * @param substr substring to find * @param startIndex starting index * @param endIndex ending index * @return index of founded substring or -1 if substring not found */ public static int indexOf(String s, String substr, int startIndex, int endIndex) { if (startIndex < 0) { startIndex = 0; } int srclen = s.length(); if (endIndex > srclen) { endIndex = srclen; } int sublen = substr.length(); if (sublen == 0) { return startIndex > srclen ? srclen : startIndex; } int total = endIndex - sublen + 1; char c = substr.charAt(0); mainloop: for (int i = startIndex; i < total; i++) { if (s.charAt(i) != c) { continue; } int j = 1; int k = i + 1; while (j < sublen) { if (substr.charAt(j) != s.charAt(k)) { continue mainloop; } j++; k++; } return i; } return -1; } /** * Finds the first occurrence of a character in the given source but within limited range (start, end]. */ public static int indexOf(String s, char c, int startIndex, int endIndex) { if (startIndex < 0) { startIndex = 0; } int srclen = s.length(); if (endIndex > srclen) { endIndex = srclen; } for (int i = startIndex; i < endIndex; i++) { if (s.charAt(i) == c) { return i; } } return -1; } /** * Finds the very first index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> if * noting found. * * @param s source string * @param arr string array */ public static int[] indexOf(String s, String arr[]) { return indexOf(s, arr, 0); } /** * Finds the very first index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> * if noting found. * * @param s source string * @param arr string array * @param start starting position */ public static int[] indexOf(String s, String arr[], int start) { int arrLen = arr.length; int index = Integer.MAX_VALUE; int last = -1; for (int j = 0; j < arrLen; j++) { int i = s.indexOf(arr[j], start); if (i != -1) { if (i < index) { index = i; last = j; } } } return last == -1 ? null : new int[] { last, index }; } /** * Finds the very first index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> if * noting found. * * @param s source string * @param c char array */ public static int[] indexOf(String s, char c[]) { return indexOf(s, c, 0); } /** * Finds the very first index of a char from the specified array. It * returns an int[2] where int[0] represents the char index and int[1] * represents position where char was found. Returns <code>null</code> * if noting found. * * @param s source string * @param c char array * @param start starting position */ public static int[] indexOf(String s, char c[], int start) { int arrLen = c.length; int index = Integer.MAX_VALUE; int last = -1; for (int j = 0; j < arrLen; j++) { int i = s.indexOf(c[j], start); if (i != -1) { if (i < index) { index = i; last = j; } } } return last == -1 ? null : new int[] { last, index }; } /** * <p> * Gets a substring from the specified String avoiding exceptions. * </p> * * <p> * A negative start position can be used to start {@code n} characters from * the end of the String. * </p> * * <p> * A {@code null} String will return {@code null}. An empty ("") String will * return "". * </p> * */ public static String substring(final String s, int start) { if (s == null) { return null; } // handle negatives, which means last n characters if (start < 0) { start = s.length() + start; // remember start is negative } if (start < 0) { start = 0; } if (start > s.length()) { return ""; } return s.substring(start); } /** * <p> * Gets a substring from the specified String avoiding exceptions. * </p> * * <p> * A negative start position can be used to start/end {@code n} characters * from the end of the String. * </p> * * <p> * The returned substring starts with the character in the {@code start} * position and ends before the {@code end} position. All position counting * is zero-based -- i.e., to start at the beginning of the string use * {@code start = 0}. Negative start and end positions can be used to * specify offsets relative to the end of the String. * </p> * * <p> * If {@code start} is not strictly to the left of {@code end}, "" is * returned. * </p> * */ public static String substring(final String s, int start, int end) { if (s == null) { return null; } // handle negatives if (end < 0) { end = s.length() + end; // remember end is negative } if (start < 0) { start = s.length() + start; // remember start is negative } // check length next if (end > s.length()) { end = s.length(); } // if start is greater than end, return "" if (start > end) { return ""; } if (start < 0) { start = 0; } if (end < 0) { end = 0; } return s.substring(start, end); } }