/**
* A network library for processing which supports UDP, TCP and Multicast.
*
* (c) 2004-2012
*
* 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
*
* @author Andreas Schlegel http://www.sojamo.de
* @modified 12/23/2012
* @version 0.9.9
*/
/**
* StringUtils Contains some basic utility methods for handling Strings.
*
* Copyright (C) 2003 Johan Känngård
* Contains code Copyright (C) 2001,2002 Stephen Ostermiller
* http://ostermiller.org/utils/StringHelper.java.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The GPL is located at: http://www.gnu.org/licenses/gpl.txt
*
* @author Johan Känngård, http://dev.kanngard.net/
* @version 0.4
*/
package netP5;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;
/**
* @invisible
*/
public class StringUtils extends Object {
/**
* Protected because this class does only contain static methods.
*/
protected StringUtils() {
}
/**
* Returns the substring to the right of the specified substring in the
* specified String, starting from the left.
*
* @param source
* the source String to search.
* @param searchFor
* the substring to search for in source.
* @return the substring that is to the right of searchFor in source.
*/
public static String right(String source, String searchFor) {
int index = source.indexOf(searchFor) + searchFor.length();
if (index < 0) {
return "";
}
return source.substring(index);
}
/**
* Returns the substring to the right of the specified substring in the
* specified String, starting from the right.
*
* @param source
* the source String to search.
* @param searchFor
* the substring to search for in source.
* @return the substring that is to the right of searchFor in source,
* starting from the right.
*/
public static String rightBack(String source, String searchFor) {
int index = source.lastIndexOf(searchFor) + searchFor.length();
if (index < 0) {
return "";
}
return source.substring(index);
}
/**
* Returns the substring to the left of the specified substring in the
* specified String, starting from the left.
*
* @param source
* the source String to search.
* @param searchFor
* the substring to search for in source.
* @return the substring that is to the left of searchFor in source.
*/
public static String left(String source, String searchFor) {
int index = source.indexOf(searchFor);
if (index <= 0) {
return "";
}
return source.substring(0, index);
}
/**
* Returns the substring to the left of the specified substring in the
* specified String, starting from the right.
*
* @param source
* the source String to search.
* @param searchFor
* the substring to search for in source.
* @return the substring that is to the left of searchFor in source,
* starting from the right.
*/
public static String leftBack(String source, String searchFor) {
int index = source.lastIndexOf(searchFor);
if (index <= 0) {
return "";
}
return source.substring(0, index);
}
/**
* Returns the substring between two substrings. I.e.
* StringUtils.middle("This i a big challenge", "a", "challenge") returns "
* big ".
*
* @param source
* the String to search.
* @param start
* the String to the left to search for, from the left.
* @param end
* the String to the right to search for, from the right.
*/
public static String middle(String source, String start, String end) {
String one = StringUtils.right(source, start);
return StringUtils.leftBack(one, end);
}
/**
* Returns a substring of a String, starting from specified index and with
* specified length. I. e. StringUtils.middle("This is a big challenge", 5,
* 6) returns " is a "
*
* @param source
* the String to get a substring from.
* @param startIndex
* the index in the source String to get the substring from.
* @param length
* the length of the substring to return.
*/
public static String middle(String source, int startIndex, int length) {
return source.substring(startIndex, source.length() - length);
}
/**
* Replaces substrings in a string.
*
* @param source
* the source String to replace substrings in.
* @param searchFor
* the string to search for.
* @param replaceWith
* the string to replace all found searchFor-substrings with.
*/
public static String replace(String source, String searchFor,
String replaceWith) {
if (source.length() < 1) {
return "";
}
int p = 0;
while (p < source.length() && (p = source.indexOf(searchFor, p)) >= 0) {
source = source.substring(0, p) + replaceWith
+ source.substring(p + searchFor.length(), source.length());
p += replaceWith.length();
}
return source;
}
/**
* Replaces several substrings in a string.
*
* @param source
* the source String to replace substrings in.
* @param searchFor
* the substrings to search for.
* @param replaceWith
* what to replace every searchFor with,
*/
public static String replace(String source, String[] searchFor,
String replaceWith) {
for (int i = 0; i < searchFor.length; i++) {
StringUtils.replace(source, searchFor[i], replaceWith);
}
return source;
}
/**
* Splits every String in an array at the specified lengths.
*
* Example:
The result that will be output: 123 a 123 b
*
* Bla 1 bla 2
*
* @return a Vector containing String arrays (the rows).
*/
public static Vector explode(String[] source, int[] lengths) {
Vector v = new Vector();
for (int i = 0; i < source.length; i++) {
v.addElement(StringUtils.explode(source[i], lengths));
}
return v;
}
/**
* Splits a string at the specified lengths and returns an array of Strings.
*
* @param source
* the String to split.
* @lengths an array of lengths where to split the String.
* @return an array of Strings with the same number of elements as the
* number of elements in the lengths argument. The length of each
* String element is specified by the correspondent lengths array
* element.
* @throws IndexOutOfBoundsException
* if any of the length´s are invalid.
*/
public static String[] explode(String source, int[] lengths) {
String[] result = new String[lengths.length];
int position = 0;
for (int i = 0; i < lengths.length; i++) {
if (lengths[i] + position > source.length()) {
throw new IndexOutOfBoundsException();
}
result[i] = source.substring(position, position + lengths[i]);
position += lengths[i];
}
return result;
}
/**
* Splits a string into an array with a space as delimiter.
*
* @param source
* the source String to explode.
* @return an array of strings that are made out of splitting the string at
* the spaces.
*/
public static String[] explode(String source) {
return StringUtils.explode(source, " ");
}
/**
* Splits a string into an array with the specified delimiter. Original code
* Copyright (C) 2001,2002 Stephen Ostermiller
* http://ostermiller.org/utils/StringHelper.java.html
*
*
* String source[] = { "123a123b123c123d", "Bla1bla2bla3bla4bla5bla6bla7" };
* int[] lengths = { 3, 1, 3, 1 };
* Vector result = StringUtils.explode(source, lengths);
* Object element = null;
* String[] rowElements = null;
* Enumeration enum = result.elements();
* while (enum.hasMoreElements()) {
* element = enum.nextElement();
* if (element instanceof String[]) {
* rowElements = (String[]) element;
* for (int i = 0; i < rowElements.length; i++) {
* System.out.println(rowElements[i]);
* }
* }
* }
*
* This method is meant to be similar to the split function in other * programming languages but it does not use regular expressions. Rather the * String is split on a single String literal. It is equivalent to the * * @Explode function in Lotus Notes / Domino. *
** Unlike java.util.StringTokenizer which accepts multiple * character tokens as delimiters, the delimiter here is a single * String literal. *
** Each null token is returned as an empty String. Delimiters are * never returned as tokens. *
** If there is no delimiter because it is either empty or null, the * only element in the result is the original String. *
*
* StringHelper.explode("1-2-3", "-");
* result: {"1", "2", "3"}
* StringHelper.explode("-1--2-", "-");
* result: {"", "1", ,"", "2", ""}
* StringHelper.explode("123", "");
* result: {"123"}
* StringHelper.explode("1-2---3----4", "--");
* result: {"1-2", "-3", "", "4"}
*