org.ijsberg.iglu.util.misc
Class StringSupport

java.lang.Object
  extended by org.ijsberg.iglu.util.misc.StringSupport

public abstract class StringSupport
extends Object

Helper class containing string manipulation functions.


Constructor Summary
StringSupport()
           
 
Method Summary
static String absorbInputStream(InputStream input)
          keep reading until the InputStream is exhausted
static String absorbInputStream(InputStream input, String encoding)
          keep reading until the InputStream is exhausted
static String condenseWhitespace(String haystack)
           
static void condenseWhitespace(StringBuffer haystack)
           
static boolean containsCharacters(String in, String chars)
          tells whether one or more characters in a String are part of a given String
static boolean containsOnlyCharacters(String in, String chars)
          tells whether all characters in a String are part of a given String
static int count(String haystack, String needle)
           
static char[] createCharArray(int size, char defaultVal)
          Creates a character array and initializes it with a default value.
static String esc(String in)
          Escapes quotes, double quotes, ecape characters and end-of-line characters in strings.
static void esc(StringBuffer in)
          Escapes quotes, double quotes, ecape characters and end-of-line characters in strings
static Set extractStringsInbetweenTagsFromText(String input, char startTag, char endTag, boolean sort)
          reads all strings in a text that are in between certain tags such as '[' and ']'
static String getStackTrace(Throwable t)
          Retrieves stack trace from throwable
static String getStackTrace(Throwable t, int depth)
          Retrieves stack trace from throwable.
static String getStackTrace(Throwable t, int depth, String prefix)
          Retrieves stack trace from throwable.
static String insert(String haystack, String needle, int index)
          replaces the first occurrence of needle in haystack with newNeedle
static boolean isAlpha(String in)
          tells whether all characters in a String are letters
static boolean isAlphaNumeric(String in)
          tells whether all characters in a String are digits or letters
static boolean isAlphaNumericOrContainsOnlyCharacters(String in, String chars)
          tells whether all characters in a String are letters or digits or part of a given String
static boolean isNumeric(String in)
          tells whether all characters in a String are digits
static void removeAll(StringBuffer haystack, String needle)
          removes all occurances of needle in haystack
static String removeAll(String haystack, String needle)
          removes all occurances of needle in haystack
static void replaceAll(StringBuffer haystack, String needle, String newNeedle)
          replaces all occurances of needle in haystack with newNeedle the input itself is not modified
static void replaceAll(StringBuffer haystack, String needle, String newNeedle, int interval)
           
static String replaceAll(String haystack, String[] needle, String[] newNeedle)
          Replaces a series of possible occurrences by a series of substitutes.
static String replaceAll(String haystack, String needle, String newNeedle)
          replaces all occurances of needle in haystack with newNeedle the input itself is not modified
static String replaceAll(String haystack, String needle, String newNeedle, int interval)
           
static void replaceFirst(StringBuffer haystack, String needle, String newNeedle)
          replaces the first occurance of needle in haystack with newNeedle
static String replaceFirst(String haystack, String needle, String newNeedle)
           
static void replaceLast(StringBuffer haystack, String needle, String newNeedle)
          replaces the last occurance of needle in haystack with newNeedle
static String replaceLast(String haystack, String needle, String newNeedle)
           
static List<String> split(String input)
          reads all words in a text, words in this case are a series of characters seperated by SPACE, ',', '.' or ':'
static List<String> split(String input, String punctuationChars)
          reads all words in a text, words in this case are a series of characters seperated by SPACE, ',', '.' or ':'
static List<String> split(String input, String punctuationChars, boolean sort)
          reads all words in a text and converts them to lower case
static List<String> split(String input, String punctuationChars, boolean sort, boolean convertToLowerCase)
          reads all words in a text
static List<String> split(String input, String punctuationChars, boolean sort, boolean convertToLowerCase, boolean distinct)
          reads all words in a text
static List<String> split(String input, String punctuationChars, String quoteChars)
          reads all words in a text and converts them to lower case
static List<String> split(String input, String punctuationChars, String quoteSymbols, boolean sort, boolean convertToLowerCase, boolean distinct)
          reads all words in a text
static String trim(String input, int maxNrofChars, String end)
          Trims strings that contain too many characters and adds trailing characters to indicate that the original string has been trimmed.
static void writeToOutputStream(String s, OutputStream output)
          Writes the contents of a string to an output stream.
static void writeToOutputStream(String s, OutputStream output, String encoding)
          Writes the contents of a string to an output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringSupport

public StringSupport()
Method Detail

insert

public static String insert(String haystack,
                            String needle,
                            int index)
replaces the first occurrence of needle in haystack with newNeedle

Parameters:
haystack - input string
needle - string to place
index - position to place

replaceFirst

public static String replaceFirst(String haystack,
                                  String needle,
                                  String newNeedle)

replaceFirst

public static void replaceFirst(StringBuffer haystack,
                                String needle,
                                String newNeedle)
replaces the first occurance of needle in haystack with newNeedle

Parameters:
haystack - input string
needle - string to replace
newNeedle - replacement

replaceLast

public static String replaceLast(String haystack,
                                 String needle,
                                 String newNeedle)

replaceLast

public static void replaceLast(StringBuffer haystack,
                               String needle,
                               String newNeedle)
replaces the last occurance of needle in haystack with newNeedle

Parameters:
haystack - input string
needle - string to replace
newNeedle - replacement

replaceAll

public static String replaceAll(String haystack,
                                String needle,
                                String newNeedle)
replaces all occurances of needle in haystack with newNeedle the input itself is not modified

Parameters:
haystack - input string
needle - string to replace
newNeedle - replacement
See Also:
use StringBuffer input to do a series of invocations in a row with minimal performance impact

replaceAll

public static String replaceAll(String haystack,
                                String needle,
                                String newNeedle,
                                int interval)

replaceAll

public static String replaceAll(String haystack,
                                String[] needle,
                                String[] newNeedle)
Replaces a series of possible occurrences by a series of substitutes.

Parameters:
haystack -
needle -
newNeedle -
Returns:

replaceAll

public static void replaceAll(StringBuffer haystack,
                              String needle,
                              String newNeedle)
replaces all occurances of needle in haystack with newNeedle the input itself is not modified

Parameters:
haystack - input string
needle - string to replace
newNeedle - replacement

replaceAll

public static void replaceAll(StringBuffer haystack,
                              String needle,
                              String newNeedle,
                              int interval)

count

public static int count(String haystack,
                        String needle)

condenseWhitespace

public static String condenseWhitespace(String haystack)

condenseWhitespace

public static void condenseWhitespace(StringBuffer haystack)

removeAll

public static String removeAll(String haystack,
                               String needle)
removes all occurances of needle in haystack

Parameters:
haystack - input string
needle - string to remove

removeAll

public static void removeAll(StringBuffer haystack,
                             String needle)
removes all occurances of needle in haystack

Parameters:
haystack - input string
needle - string to remove

esc

public static String esc(String in)
Escapes quotes, double quotes, ecape characters and end-of-line characters in strings.

Parameters:
in - input string
Returns:
escaped string

esc

public static void esc(StringBuffer in)
Escapes quotes, double quotes, ecape characters and end-of-line characters in strings

Parameters:
in - input string

isNumeric

public static boolean isNumeric(String in)
tells whether all characters in a String are digits

Parameters:
in - String to evaluate

isAlpha

public static boolean isAlpha(String in)
tells whether all characters in a String are letters

Parameters:
in - String to evaluate

isAlphaNumeric

public static boolean isAlphaNumeric(String in)
tells whether all characters in a String are digits or letters

Parameters:
in - String to evaluate

isAlphaNumericOrContainsOnlyCharacters

public static boolean isAlphaNumericOrContainsOnlyCharacters(String in,
                                                             String chars)
tells whether all characters in a String are letters or digits or part of a given String

Parameters:
in - String to evaluate
chars - characters which are allowed in the given String

containsCharacters

public static boolean containsCharacters(String in,
                                         String chars)
tells whether one or more characters in a String are part of a given String

Parameters:
in - String to evaluate
chars - characters which are to be tested in the given String

containsOnlyCharacters

public static boolean containsOnlyCharacters(String in,
                                             String chars)
tells whether all characters in a String are part of a given String

Parameters:
in - String to evaluate
chars - characters which are to be tested in the given String

absorbInputStream

public static String absorbInputStream(InputStream input)
                                throws IOException
keep reading until the InputStream is exhausted

Parameters:
input -
Returns:
a resulting String
Throws:
IOException

absorbInputStream

public static String absorbInputStream(InputStream input,
                                       String encoding)
                                throws IOException
keep reading until the InputStream is exhausted

Parameters:
input -
Returns:
a resulting String
Throws:
IOException

writeToOutputStream

public static void writeToOutputStream(String s,
                                       OutputStream output)
                                throws IOException
Writes the contents of a string to an output stream.

Parameters:
s -
output -
Throws:
IOException

writeToOutputStream

public static void writeToOutputStream(String s,
                                       OutputStream output,
                                       String encoding)
                                throws IOException
Writes the contents of a string to an output stream.

Parameters:
s -
output -
encoding -
Throws:
IOException

split

public static List<String> split(String input)
reads all words in a text, words in this case are a series of characters seperated by SPACE, ',', '.' or ':'

Parameters:
input -
Returns:
a collection of uniquely identified words in order of appearance

split

public static List<String> split(String input,
                                 String punctuationChars)
reads all words in a text, words in this case are a series of characters seperated by SPACE, ',', '.' or ':'

Parameters:
input -
punctuationChars -
Returns:
a collection of uniquely identified words

split

public static List<String> split(String input,
                                 String punctuationChars,
                                 boolean sort)
reads all words in a text and converts them to lower case

Parameters:
input -
punctuationChars - characters that can not belong to words and are therefore separators
sort - indicates if result must be sorted alphabetically
Returns:
a collection of uniquely identified words

split

public static List<String> split(String input,
                                 String punctuationChars,
                                 String quoteChars)
reads all words in a text and converts them to lower case

Parameters:
input -
punctuationChars - characters that can not belong to words and are therefore separators
Returns:
a collection of uniquely identified words

split

public static List<String> split(String input,
                                 String punctuationChars,
                                 boolean sort,
                                 boolean convertToLowerCase)
reads all words in a text

Parameters:
input -
punctuationChars - characters that can not belong to words and are therefore separators
sort - whether to sort the result alphabetically
convertToLowerCase - whether to convert all found words to lowercase
Returns:
a collection of uniquely identified words

split

public static List<String> split(String input,
                                 String punctuationChars,
                                 boolean sort,
                                 boolean convertToLowerCase,
                                 boolean distinct)
reads all words in a text

Parameters:
input -
punctuationChars - characters that can not belong to words and are therefore separators
sort - whether to sort the result alphabetically. (If the result is sorted, setting the distinct flag to false has no effect)
convertToLowerCase - whether to convert all found words to lowercase
distinct - true if a certain word may occur only once in the resulting collection
Returns:
a collection of extracted words

split

public static List<String> split(String input,
                                 String punctuationChars,
                                 String quoteSymbols,
                                 boolean sort,
                                 boolean convertToLowerCase,
                                 boolean distinct)
reads all words in a text

Parameters:
input -
punctuationChars - characters that can not belong to words and are therefore separators
quoteSymbols - used as list of characters used to group strings
sort - whether to sort the result alphabetically. (If the result is sorted, setting the distinct flag to false has no effect)
convertToLowerCase - whether to convert all found words to lower case
distinct - true if a certain word may occur only once in the resulting collection
Returns:
a collection of extracted words

extractStringsInbetweenTagsFromText

public static Set extractStringsInbetweenTagsFromText(String input,
                                                      char startTag,
                                                      char endTag,
                                                      boolean sort)
reads all strings in a text that are in between certain tags such as '[' and ']'

Parameters:
input -
startTag -
endTag -
sort - whether to sort the result alphabetically
Returns:
a collection of uniquely identified words

getStackTrace

public static String getStackTrace(Throwable t)
Retrieves stack trace from throwable

Parameters:
t -
Returns:

getStackTrace

public static String getStackTrace(Throwable t,
                                   int depth)
Retrieves stack trace from throwable.

Parameters:
t -
depth -
Returns:

getStackTrace

public static String getStackTrace(Throwable t,
                                   int depth,
                                   String prefix)
Retrieves stack trace from throwable.

Parameters:
t -
depth -
prefix -
Returns:

trim

public static String trim(String input,
                          int maxNrofChars,
                          String end)
Trims strings that contain too many characters and adds trailing characters to indicate that the original string has been trimmed.

Parameters:
input -
maxNrofChars - maximum allowed length of input
end - trailing characters when trimmed
Returns:
original string or trimmed version

createCharArray

public static char[] createCharArray(int size,
                                     char defaultVal)
Creates a character array and initializes it with a default value.

Parameters:
size -
defaultVal -
Returns:


Copyright © 2011. All Rights Reserved.