Java tutorial
//package com.java2s; /* * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * to you 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.StringTokenizer; public class Main { /** * <p> * Splits the provided text into a array, using whitespace as the separator. * </p> * <p/> * <p> * The separator is not included in the returned String array. * </p> * * @param str the String to parse * @return an array of parsed Strings */ public static String[] split(String str) { return split(str, null, -1); } /** * @see #split(String, String, int) */ public static String[] split(String text, String separator) { return split(text, separator, -1); } /** * <p> * Splits the provided text into a array, based on a given separator. * </p> * <p/> * <p> * The separator is not included in the returned String array. The maximum number of splits to perfom can be * controlled. A <code>null</code> separator will cause parsing to be on whitespace. * </p> * <p/> * <p> * This is useful for quickly splitting a String directly into an array of tokens, instead of an enumeration of * tokens (as <code>StringTokenizer</code> does). * </p> * * @param str The string to parse. * @param separator Characters used as the delimiters. If <code>null</code>, splits on whitespace. * @param max The maximum number of elements to parse. The rest of the string to parse will be contained in the last * array element. A zero or negative value implies no limit. * @return an array of parsed Strings */ public static String[] split(String str, String separator, int max) { StringTokenizer tok = null; if (separator == null) { // Null separator means we're using StringTokenizer's default // delimiter, which comprises all whitespace characters. tok = new StringTokenizer(str); } else { tok = new StringTokenizer(str, separator); } int listSize = tok.countTokens(); if ((max > 0) && (listSize > max)) { listSize = max; } String[] list = new String[listSize]; int i = 0; int lastTokenBegin = 0; int lastTokenEnd = 0; while (tok.hasMoreTokens()) { if ((max > 0) && (i == listSize - 1)) { // In the situation where we hit the max yet have // tokens left over in our input, the last list // element gets all remaining text. String endToken = tok.nextToken(); lastTokenBegin = str.indexOf(endToken, lastTokenEnd); list[i] = str.substring(lastTokenBegin); break; } else { list[i] = tok.nextToken(); lastTokenBegin = str.indexOf(list[i], lastTokenEnd); lastTokenEnd = lastTokenBegin + list[i].length(); } i++; } return list; } /** * <p> * Gets a substring from the specified string avoiding exceptions. * </p> * <p/> * <p> * A negative start position can be used to start <code>n</code> characters from the end of the String. * </p> * * @param str the String to get the substring from * @param start the position to start from, negative means count back from the end of the String by this many * characters * @return substring from start position */ public static String substring(String str, int start) { if (str == null) { return null; } // handle negatives, which means last n characters if (start < 0) { start = str.length() + start; // remember start is negative } if (start < 0) { start = 0; } if (start > str.length()) { return ""; } return str.substring(start); } /** * <p> * Gets a substring from the specified String avoiding exceptions. * </p> * <p/> * <p> * A negative start position can be used to start/end <code>n</code> characters from the end of the String. * </p> * * @param str the String to get the substring from * @param start the position to start from, negative means count back from the end of the string by this many * characters * @param end the position to end at (exclusive), negative means count back from the end of the String by this many * characters * @return substring from start position to end positon */ public static String substring(String str, int start, int end) { if (str == null) { return null; } // handle negatives if (end < 0) { end = str.length() + end; // remember end is negative } if (start < 0) { start = str.length() + start; // remember start is negative } // check length next if (end > str.length()) { // check this works. end = str.length(); } // if start is greater than end, return "" if (start > end) { return ""; } if (start < 0) { start = 0; } if (end < 0) { end = 0; } return str.substring(start, end); } }