net.sf.mmapps.commons.util
Class StringUtil

java.lang.Object
  extended by net.sf.mmapps.commons.util.StringUtil

public class StringUtil
extends java.lang.Object

Author:
Nico Klasens

Constructor Summary
StringUtil()
           
 
Method Summary
static java.lang.String addLeadingChar(java.lang.String str, char ch, int total)
          Add Leading Char.to the string
static int countOccurences(java.lang.String haystack, java.lang.String needle)
          This method counts the occurences of 'needle' in the string 'haystack'.
static int countOccurences(java.lang.String haystack, java.lang.String needle, int position)
          This method counts the occurences of 'needle' in the string 'haystack' starting from position 'position'.
static java.lang.String escapeXml(java.lang.String str)
           
static boolean isEmpty(java.lang.String str)
          is Empty.String
static boolean isEmptyOrWhitespace(java.lang.String str)
          is Empty Or Whitespace.String
static java.lang.String randomString(int len, char begin, char end)
          Generates a String with length 'len', filled with random(ish) characters from 'begin' to 'end'. e.g. randomString(3, 'a', 'm') can return "kgm", "faa", "dlb", etc. but never "kgn" (because 'n' doesn't lie between 'a' and 'm')
static java.lang.String removeWhitespaces(java.lang.String s)
          Remove all whitespaces from a string
static java.lang.String replace(java.lang.String str, java.lang.String oldToken, java.lang.String newToken)
          Transforms a string by replacing every occurence of a given searchstring with another string.
static java.lang.String replaceIgnoreCase(java.lang.String str, java.lang.String oldToken, java.lang.String newToken)
          Transforms a string by replacing every occurence of a given searchstring with another string.
static java.lang.String truncate(java.lang.String str, int maxLength)
          Returns a new string with the given maximum length of the given string, with "..." appended to the end if the maximum length was exceeded.
static java.lang.String wrap(java.lang.String str, int lineLength)
          Wraps the given string, using the java.text.BreakIterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtil

public StringUtil()
Method Detail

isEmpty

public static boolean isEmpty(java.lang.String str)
is Empty.String

Parameters:
str - String to check emptiness
Returns:
boolean is it empty

isEmptyOrWhitespace

public static boolean isEmptyOrWhitespace(java.lang.String str)
is Empty Or Whitespace.String

Parameters:
str - String to check emptiness
Returns:
boolean is it empty

addLeadingChar

public static java.lang.String addLeadingChar(java.lang.String str,
                                              char ch,
                                              int total)
Add Leading Char.to the string

Parameters:
str - String to add character in front of
ch - character to add
total - total length of output string
Returns:
String with leading characters

replace

public static java.lang.String replace(java.lang.String str,
                                       java.lang.String oldToken,
                                       java.lang.String newToken)
Transforms a string by replacing every occurence of a given searchstring with another string.
If the string or searchstring is empty then the string is returned without modifictions. The searchstring occurences are deleted from the string if the replace string is empty Example :

Parameters:
str - The sourcestring.
oldToken - The searchstring.
newToken - The replacestring.
Returns:
The transformed string.

replaceIgnoreCase

public static java.lang.String replaceIgnoreCase(java.lang.String str,
                                                 java.lang.String oldToken,
                                                 java.lang.String newToken)
Transforms a string by replacing every occurence of a given searchstring with another string.
If the string or searchstring is empty then the string is returned without modifictions. The searchstring occurences are deleted from the string if the replace string is empty Example :

Parameters:
str - The sourcestring.
oldToken - The searchstring.
newToken - The replacestring.
Returns:
The transformed string.

countOccurences

public static int countOccurences(java.lang.String haystack,
                                  java.lang.String needle)
This method counts the occurences of 'needle' in the string 'haystack'.

Parameters:
haystack - full string
needle - needle to search for
Returns:
number of occurences

countOccurences

public static int countOccurences(java.lang.String haystack,
                                  java.lang.String needle,
                                  int position)
This method counts the occurences of 'needle' in the string 'haystack' starting from position 'position'.

Parameters:
haystack - full string
needle - needle to search for
position - starting position
Returns:
number of occurences

escapeXml

public static java.lang.String escapeXml(java.lang.String str)

randomString

public static java.lang.String randomString(int len,
                                            char begin,
                                            char end)
Generates a String with length 'len', filled with random(ish) characters from 'begin' to 'end'. e.g. randomString(3, 'a', 'm') can return "kgm", "faa", "dlb", etc. but never "kgn" (because 'n' doesn't lie between 'a' and 'm')

Parameters:
len - the length of the desired string
begin - the first valid character in the sequence of possible characters
end - the first valid character in the sequence of possible characters
Returns:
the random(ish) string, as specified by the above parameters

wrap

public static java.lang.String wrap(java.lang.String str,
                                    int lineLength)
Wraps the given string, using the java.text.BreakIterator.

Parameters:
str - The string that might be too long
lineLength - The maximum length of the lines to be returned.
Returns:
wrapped string

truncate

public static java.lang.String truncate(java.lang.String str,
                                        int maxLength)
Returns a new string with the given maximum length of the given string, with "..." appended to the end if the maximum length was exceeded.

Parameters:
str - The string that might be too long
maxLength - The maximum length of the string to be returned, ignored if <= 0.
Returns:
truncated string

removeWhitespaces

public static java.lang.String removeWhitespaces(java.lang.String s)
Remove all whitespaces from a string

Parameters:
s - String which might contain whitespaces
Returns:
String which contains no whitespaces any more


Copyright © 2003-2006 mmapps. All Rights Reserved.