Java tutorial
//package com.java2s; /* * IRClib -- A Java Internet Relay Chat library -- class IRCUtil * Copyright (C) 2002, 2003 Christoph Schwering <ch@schwering.org> * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.util.Vector; public class Main { /** * Splits a string into substrings. * @param str The string which is to split. * @param delim The delimiter character, for example a space <code>' '</code>. * @param trailing The ending which is added as a substring though it wasn't * in the <code>str</code>. This parameter is just for the * <code>IRCParser</code> class which uses this method to * split the <code>middle</code> part into the parameters. * But as last parameter always the <code>trailing</code> is * added. This is done here because it's the fastest way to * do it here.<br /> * If the <code>end</code> is <code>null</code> or * <code>""</code>, nothing is appended. * @return An array with all substrings. * @see #split(String, int) */ public static String[] split(String str, int delim, String trailing) { Vector items = new Vector(15); int last = 0; int index = 0; int len = str.length(); while (index < len) { if (str.charAt(index) == delim) { items.add(str.substring(last, index)); last = index + 1; } index++; } if (last != len) items.add(str.substring(last)); if (trailing != null && trailing.length() != 0) items.add(trailing); String[] result = new String[items.size()]; items.copyInto(result); return result; } /** * Splits a string into substrings. This method is totally equal to * <code>split(str, delim, null)</code>. * @param str The string which is to split. * @param delim The delimiter character, for example a space <code>' '</code>. * @return An array with all substrings. * @see #split(String, int, String) */ public static String[] split(String str, int delim) { return split(str, delim, null); } }