com.senn.magic.util
Class StringUtils

java.lang.Object
  extended by com.senn.magic.util.StringUtils

public final class StringUtils
extends Object

This class contains useful and fun methods for String and text.

Since:
1.0
Author:
Bart Thierens
Last modification: 22/01/2010

Field Summary
static String OUTPUT_CONSOLE
          Line break for console output: \n\r
static String OUTPUT_HTML
          Line break for html output: <br />
 
Method Summary
static String backwards(String text)
          Flip a text backwards.
static boolean compare(String s1, String s2, boolean ignoreCase)
          This method checks if 2 strings are the same, depending on the value of the ignoreCase variable.
static Map<String,Integer> countChars(String text)
          Get all characters used in a text and their respective count.
static String getFillerText(int lineAmount, String output)
          Get a random filler text.
static List<Integer> getOccurrences(String part, String text, boolean ignoreCase)
          Get the indexes of the occurrences of a certain String inside another String.
static int getOccurrencesCount(String part, String text, boolean ignoreCase)
          Get the amount of occurrences of a certain String inside another String.
static String invertUpperCaseLowerCase(String text)
          Change all uppercase characters to lowercase and vice versa.
static boolean isValid(String value)
          Checks if a given String value is valid, meaning it is not null and the length > 0.
static List<String> split(String text, char separator)
          Split a text using a single separator.
static List<String> split(String text, char[] separators)
          Split a text using multiple separators at once.
static List<String> split(String text, List<String> separators)
          Split a text using multiple separators at once.
static List<String> split(String text, String separator)
          Split a text using a single separator.
static List<String> split(String text, String[] separators)
          Split a text using multiple separators at once.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OUTPUT_CONSOLE

public static final String OUTPUT_CONSOLE
Line break for console output: \n\r

See Also:
Constant Field Values

OUTPUT_HTML

public static final String OUTPUT_HTML
Line break for html output: <br />

See Also:
Constant Field Values
Method Detail

backwards

public static String backwards(String text)
Flip a text backwards.

Examples:
TextMagic.backwards("abc") => "cba"
TextMagic.backwards("12345") => "54321"

Parameters:
text - text to be reversed
Returns:
String - Your text flipped backwards.

getFillerText

public static String getFillerText(int lineAmount,
                                   String output)
Get a random filler text.
Can be useful to fill elements in a gui to see what it looks like with text.

Parameters:
lineAmount - number of lines you want
output - choose one of the OUTPUT_constants to indicate what output format you're using.
Using your own line-break is also possible, but might not return a correct representation of the text.

constants:
- TextMagic.OUTPUT_CONSOLE = "\n\r"
- TextMagic.OUTPUT_HTML = "< br />"
Returns:
String - a number of filler lines.

getOccurrencesCount

public static int getOccurrencesCount(String part,
                                      String text,
                                      boolean ignoreCase)
Get the amount of occurrences of a certain String inside another String.

Parameters:
part - the piece of text you want the occurrences count from
text - the containing text
ignoreCase - false for case sensitive
Returns:
int - the amount of occurrences found

getOccurrences

public static List<Integer> getOccurrences(String part,
                                           String text,
                                           boolean ignoreCase)
Get the indexes of the occurrences of a certain String inside another String.

Parameters:
part - the piece of text you want the occurrences from
text - the containing text
ignoreCase - false for case sensitive
Returns:
List - the indexes of the occurrences found

split

public static List<String> split(String text,
                                 char[] separators)
Split a text using multiple separators at once.

Examples:
TextMagic.split("a;b-c,d", new char[]{';', '-', ','}) => "a", "b", "c", "d"
TextMagic.split("a;b-c,d", new char[]{';', ','}) => "a", "b-c", "d"

Parameters:
text - the piece of text you want to split
separators - a char array containing all separators
Returns:
List - a List of String-objects containing all splitted parts

split

public static List<String> split(String text,
                                 String[] separators)
Split a text using multiple separators at once.

Examples:
TextMagic.split("a;b-c,d", new String[]{";", "-", ","}) => "a", "b", "c", "d"
TextMagic.split("a;b-c,d", new String[]{";", ","}) => "a", "b-c", "d"

Parameters:
text - the piece of text you want to split
separators - a String array containing all separators
Returns:
List - a List of String-objects containing all splitted parts

split

public static List<String> split(String text,
                                 List<String> separators)
Split a text using multiple separators at once.

Examples:
TextMagic.split("a;b-c,d", list) with list containing ";", "-", "," => "a", "b", "c", "d"
TextMagic.split("a;b-c,d", list) with list containing ";", "," => "a", "b-c", "d"

Parameters:
text - the piece of text you want to split
separators - a List of Strings containing all separators
Returns:
List - a List of String-objects containing all splitted parts

split

public static List<String> split(String text,
                                 String separator)
Split a text using a single separator.
Added for completion reasons

Examples:
TextMagic.split("a;b-c,d", "-") => "a;b", "c,d"
TextMagic.split("a;b-c,d", ";") => "a","b-c,d"
TextMagic.split("a;b-c,d", ";b-c") => "a",",d"

Parameters:
text - the piece of text you want to split
separator - the separator you want to use
Returns:
List - a List of String-objects containing all splitted parts

split

public static List<String> split(String text,
                                 char separator)
Split a text using a single separator.
Added for completion reasons

Examples:
TextMagic.split("a;b-c,d", 'b') => "a;", "-c,d"

Parameters:
text - the piece of text you want to split
separator - the separator you want to use
Returns:
List - a List of String-objects containing all splitted parts

countChars

public static Map<String,Integer> countChars(String text)
Get all characters used in a text and their respective count.

Example:
TextMagic.countChars("abcdcba") => returns a Map with a(2), b(2), c(2), d(1)

Parameters:
text - the text you want to get all the characters and their respective count from
Returns:
Map - a Map with String as key and Integer as value containing all characters + their count

invertUpperCaseLowerCase

public static String invertUpperCaseLowerCase(String text)
Change all uppercase characters to lowercase and vice versa.
If a character is not a letter, it will remain unchanged.

Example:
TextMagic.countChars("ABCdef") => "abcDEF"

Parameters:
text - the text you want to invert
Returns:
String - the same text as received through @param text but inverted

isValid

public static boolean isValid(String value)
Checks if a given String value is valid, meaning it is not null and the length > 0.

Parameters:
value -
Returns:
boolean true if the given String is valid, false if not

compare

public static boolean compare(String s1,
                              String s2,
                              boolean ignoreCase)
This method checks if 2 strings are the same, depending on the value of the ignoreCase variable. This method is exactly the same as the following:

if(ignoreCase)
    return s1.equalsIgnoreCase(s1);
else
    return s1.equals(s2);


This method is nothing but a time-saver!

Parameters:
s1 - the first string
s2 - the second string
ignoreCase - false if you want a case sensitive comparison, true if you don't
Returns:
boolean - true if the string values are equal, false if they're not