netP5
Class StringUtils

java.lang.Object
  extended by netP5.StringUtils

public class StringUtils
extends Object


Method Summary
static String arrayToString(String[] theArray)
           
static String arrayToString(String[] theArray, int theStart, int theEnd)
           
static String centerJustify(String source, int length)
          Creates a string of the given width with the given string left justified (padded by an appropriate number of spaces in front and after it).
static String duplicate(String source, int copies)
          Returns a String with the source String copied the specified number of times.
static String[] explode(String source)
          Splits a string into an array with a space as delimiter.
static Vector explode(String[] source, int[] lengths)
          Splits every String in an array at the specified lengths.
static String[] explode(String source, int[] lengths)
          Splits a string at the specified lengths and returns an array of Strings.
static String[] explode(String s, String delimiter)
          Splits a string into an array with the specified delimiter.
static float getFloat(String theString)
           
static int getInt(String theString)
           
static String getStackTrace(Throwable t)
          Prints the stacktrace to a buffer and returns the buffer as a String.
static String implode(Object[] elements)
          Combines an array to a string, using a comma and a space as delimiter.
static String implode(Object[] elements, String delimiter)
          Combines an array to a string, using the specified delimiter.
static boolean isEmpty(String s)
          Checks if a String is empty or null.
static String left(String source, String searchFor)
          Returns the substring to the left of the specified substring in the specified String, starting from the left.
static String leftBack(String source, String searchFor)
          Returns the substring to the left of the specified substring in the specified String, starting from the right.
static String leftJustify(String source, int length)
          Creates a string of the given width with the given string left justified (followed by an appropriate number of spaces).
static String middle(String source, int startIndex, int length)
          Returns a substring of a String, starting from specified index and with specified length.
static String middle(String source, String start, String end)
          Returns the substring between two substrings.
static String remove(String source, char searchFor)
          Removes all instances of a character in a String.
static String remove(String source, String searchFor)
          Removes all instances of a substring in a String.
static String remove(String source, String[] searchFor)
          Removes all instances of substrings in a String.
static String removeDuplicates(String source, String searchFor)
          Removes duplicates of a substring in a String.
static String replace(String source, String[] searchFor, String replaceWith)
          Replaces several substrings in a string.
static String replace(String source, String searchFor, String replaceWith)
          Replaces substrings in a string.
static String right(String source, String searchFor)
          Returns the substring to the right of the specified substring in the specified String, starting from the left.
static String rightBack(String source, String searchFor)
          Returns the substring to the right of the specified substring in the specified String, starting from the right.
static String rightJustify(String source, int length)
          Creates a string of the given width with the given string right justified (with an appropriate number of spaces before it).
static String[] slice(int theNum, String[] theStringArray)
           
static String spaces(int length)
          Returns a String with the specified number of spaces.
static char switchCase(char source)
          Switches the case of the supplied character.
static String switchCase(String source)
          Switches the case of the supplied String.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

right

public static String right(String source,
                           String searchFor)
Returns the substring to the right of the specified substring in the specified String, starting from the left.

Parameters:
source - the source String to search.
searchFor - the substring to search for in source.
Returns:
the substring that is to the right of searchFor in source.

rightBack

public static String rightBack(String source,
                               String searchFor)
Returns the substring to the right of the specified substring in the specified String, starting from the right.

Parameters:
source - the source String to search.
searchFor - the substring to search for in source.
Returns:
the substring that is to the right of searchFor in source, starting from the right.

left

public static String left(String source,
                          String searchFor)
Returns the substring to the left of the specified substring in the specified String, starting from the left.

Parameters:
source - the source String to search.
searchFor - the substring to search for in source.
Returns:
the substring that is to the left of searchFor in source.

leftBack

public static String leftBack(String source,
                              String searchFor)
Returns the substring to the left of the specified substring in the specified String, starting from the right.

Parameters:
source - the source String to search.
searchFor - the substring to search for in source.
Returns:
the substring that is to the left of searchFor in source, starting from the right.

middle

public static String middle(String source,
                            String start,
                            String end)
Returns the substring between two substrings. I.e. StringUtils.middle("This i a big challenge", "a", "challenge") returns " big ".

Parameters:
source - the String to search.
start - the String to the left to search for, from the left.
end - the String to the right to search for, from the right.

middle

public static String middle(String source,
                            int startIndex,
                            int length)
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 "

Parameters:
source - the String to get a substring from.
startIndex - the index in the source String to get the substring from.
length - the length of the substring to return.

replace

public static String replace(String source,
                             String searchFor,
                             String replaceWith)
Replaces substrings in a string.

Parameters:
source - the source String to replace substrings in.
searchFor - the string to search for.
replaceWith - the string to replace all found searchFor-substrings with.

replace

public static String replace(String source,
                             String[] searchFor,
                             String replaceWith)
Replaces several substrings in a string.

Parameters:
source - the source String to replace substrings in.
searchFor - the substrings to search for.
replaceWith - what to replace every searchFor with,

explode

public static Vector explode(String[] source,
                             int[] lengths)
Splits every String in an array at the specified lengths. Example:
 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]);
                }
        }
 }
 
The result that will be output: 123 a 123 b Bla 1 bla 2

Returns:
a Vector containing String arrays (the rows).

explode

public static String[] explode(String source,
                               int[] lengths)
Splits a string at the specified lengths and returns an array of Strings.

Parameters:
source - the String to split.
Returns:
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.

explode

public static String[] explode(String source)
Splits a string into an array with a space as delimiter.

Parameters:
source - the source String to explode.
Returns:
an array of strings that are made out of splitting the string at the spaces.

explode

public static String[] explode(String s,
                               String delimiter)
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

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

Parameters:
s - the String to explode.
delimiter - the delimiter where to split the string.
Returns:
an array of strings that are made out of splitting the string at the specified delimiter.
Throws:
NullPointerException - if s is null.

slice

public static String[] slice(int theNum,
                             String[] theStringArray)

implode

public static String implode(Object[] elements,
                             String delimiter)
Combines an array to a string, using the specified delimiter.

Parameters:
elements - the array to combine to a single string.
delimiter - the delimiter to put between the combined elements.
Returns:
the array combined to a string.

implode

public static String implode(Object[] elements)
Combines an array to a string, using a comma and a space as delimiter.

Parameters:
elements - the array to combine to a single string.
Returns:
the array combined to a string.

remove

public static String remove(String source,
                            char searchFor)
Removes all instances of a character in a String.

Parameters:
source - the String to remove substring in.
searchFor - the character to remove.
Returns:
the replaced String.

remove

public static String remove(String source,
                            String searchFor)
Removes all instances of a substring in a String.

Parameters:
source - the String to remove substring in.
searchFor - the substring to remove.
Returns:
the replaced String.

remove

public static String remove(String source,
                            String[] searchFor)
Removes all instances of substrings in a String.

Parameters:
source - the String to remove substrings in.
searchFor - an array of substrings to remove from the source String.
Returns:
the replaced String.

removeDuplicates

public static String removeDuplicates(String source,
                                      String searchFor)
Removes duplicates of a substring in a String. Case sensitive.

Parameters:
source - the String to remove duplicates in.
searchFor - the substring that can only occur one at a time, several can exist in the source though.

getStackTrace

public static String getStackTrace(Throwable t)
                            throws IOException
Prints the stacktrace to a buffer and returns the buffer as a String.

Parameters:
t - the Throwable you wnat to generate a stacktrace for.
Returns:
the stacktrace of the supplied Throwable.
Throws:
IOException

isEmpty

public static boolean isEmpty(String s)
Checks if a String is empty or null.

Parameters:
s - the String to test if it is empty or null.
Returns:
true if the String is null or empty ("").

leftJustify

public static String leftJustify(String source,
                                 int length)
Creates a string of the given width with the given string left justified (followed by an appropriate number of spaces).

Parameters:
source - the String to justify
length - the length of the resulting String
Returns:
the source String padded with spaces to fill up the length. If the source string is longer than the length argument, the source String is returned.

rightJustify

public static String rightJustify(String source,
                                  int length)
Creates a string of the given width with the given string right justified (with an appropriate number of spaces before it).

Parameters:
source - the String to justify
length - the length of the resulting String
Returns:
the source String padded with spaces to fill up the length. If the source string is longer than the length argument, the source String is returned.

centerJustify

public static String centerJustify(String source,
                                   int length)
Creates a string of the given width with the given string left justified (padded by an appropriate number of spaces in front and after it).

Parameters:
source - the String to justify
length - the length of the resulting String
Returns:
the source String padded with spaces to fill up the length. If the source string is longer than the length argument, the source String is returned.

spaces

public static String spaces(int length)
Returns a String with the specified number of spaces.

Parameters:
length - the number of spaces to return.
Returns:
a String consisting of the specified number of spaces.

duplicate

public static String duplicate(String source,
                               int copies)
Returns a String with the source String copied the specified number of times.

Parameters:
source - the source String to copy.
length - the number of copies of source to return.
Returns:
a String consisting of the specified source String copied the specified number of times.

switchCase

public static String switchCase(String source)
Switches the case of the supplied String. Any lower case characters will be uppercase and vice versa.

Parameters:
source - the String to switch case of.
Returns:
the supplied String with switched case.

switchCase

public static char switchCase(char source)
Switches the case of the supplied character. A lower case character will be uppercase and vice versa.

Parameters:
source - the character to switch case of.
Returns:
the supplied character with switched case.

getInt

public static int getInt(String theString)

getFloat

public static float getFloat(String theString)

arrayToString

public static String arrayToString(String[] theArray)

arrayToString

public static String arrayToString(String[] theArray,
                                   int theStart,
                                   int theEnd)


processing library oscP5 by Andreas Schlegel. (c) 2004-2012