fc.util
Class StringUtil

java.lang.Object
  extended by fc.util.StringUtil

public final class StringUtil
extends Object

Utility functions related to java.lang.String, i.e functions that are useful but not provided by that class.


Constructor Summary
StringUtil()
           
 
Method Summary
static String arrayToString(char[] array)
          Converts a character array into a viewable comma delimited string.
static String capitalWord(String str)
          Converts the specified String to start with a capital letter.
static String dirName(String str)
          Returns the path component of the specified filename.
static String ellipsis(String str, int maxlen)
          Converts the specified String into a string with maxlen characters, using ... as the suffix to denote the missing characters.
static String ellipsis(String str, String ellipsis, int maxlen)
          Converts the specified String into a string with maxlen characters, using the specified ellipsis as the suffix to denote the missing characters.
static String escapeDoubleQuotes(String str)
          Escapes all double quotes in the specified a string with a backslash character.
static String escapeDoubleQuotes(String str, String escape)
          Escapes all double quotes in the specified a string with the specified escape character.
static String escapeSingleQuotes(String str)
          Escapes all single quotes in the specified a string with a backslash character.
static String escapeSingleQuotes(String str, String escape)
          Escapes all single quotes in the specified a string with the specified escape character.
static String fileName(String str)
          Returns the file component of specified filename.
static String fixedWidth(String str, int width)
          Converts the specified String into a fixed width string, left padding (with a blank space) or truncating on the right as necessary.
static String fixedWidth(String str, int width, HAlign align)
          Calls fixedWidth(String, int, HAlign, char) specifying the padding character as a blank space.
static String fixedWidth(String str, int width, HAlign align, char paddingChar)
          Converts the specified String into a fixed width string.
static String join(List list, String delim)
          Joins the elements of the specified list, delimited by the specified delimiter.
static String listToString(List list, String delim)
          Converts a list into a string, each item being seperated by the specified delimiter.
static String listToString(List list, String delim, String start, String end)
          Converts a list into a string, each item being seperated by the specified delimiter.
static void main(String[] args)
           
static String nullToEmpty(Object val)
          Returns an empty string is the specified argument was null, otherwise returns the value of the toString() method invoked on the specified object.
static String nullToEmpty(String val)
          Returns an empty string is the specified argument was null, otherwise returns the argument itself.
static String remove(String target, char[] chars)
          Removes all occurences of specified characters from the specified string and returns the new resulting string.
static String removeBeginningSlash(String str)
          Removes the starting (at the very beginning of the string) forward or backward slash from the specified string (if any) and returns the resulting String.
static String removeSuffix(String name)
          Removes the any file extension (.foo for example) from the specified name and returns the resulting String.
static String removeTrailingSlash(String str)
          Removes the last forward or backward slash from the specified string (if any) and returns the resulting String.
static String repeat(char c, int length)
          Returns a String containing a string of the specified character concatenated the specified number of times.
static String repeat(String str, int length)
          Returns a String containing the specified string concatenated the specified number of times.
static String repeatToWidth(String str, int length)
          Contatenates the given string so that the maximum length reached is the specified length.
static String sentenceCase(String str, String delimiters)
          Converts the specified String to be in sentence case, whereby the first letter of each word in the sentence is uppercased and all other letters are lowercased.
static List split(String str, String delim)
          Splits the string using the specified delimiters and returns the splits parts in a List.
static String viewableAscii(char c)
          A version of viewableAscii(String) that takes a single char as a parameter.
static String viewableAscii(String str)
          Converts non printable ascii characters in the specified String to escaped or readable equivalents.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtil

public StringUtil()
Method Detail

nullToEmpty

public static String nullToEmpty(String val)
Returns an empty string is the specified argument was null, otherwise returns the argument itself.


nullToEmpty

public static String nullToEmpty(Object val)
Returns an empty string is the specified argument was null, otherwise returns the value of the toString() method invoked on the specified object.


repeat

public static String repeat(char c,
                            int length)
Returns a String containing a string of the specified character concatenated the specified number of times. Returns an empty string if length is less than/equal to zero

Parameters:
c - the character to be repeated
length - the repeat length

repeat

public static String repeat(String str,
                            int length)
Returns a String containing the specified string concatenated the specified number of times.

Parameters:
str - the string to be repeated
length - the repeat length

repeatToWidth

public static String repeatToWidth(String str,
                                   int length)
Contatenates the given string so that the maximum length reached is the specified length. If the specified string does not fit in the length, then the string is truncated appropriately.

Parameters:
str - the string to repeat
length - the length of the returned string

fixedWidth

public static String fixedWidth(String str,
                                int width)
Converts the specified String into a fixed width string, left padding (with a blank space) or truncating on the right as necessary. (i.e., the specified string is aligned left w.r.t to the specified width).

Parameters:
str - the target string
width - the fixed width
Returns:
the transformed string

fixedWidth

public static String fixedWidth(String str,
                                int width,
                                HAlign align)
Calls fixedWidth(String, int, HAlign, char) specifying the padding character as a blank space.


fixedWidth

public static String fixedWidth(String str,
                                int width,
                                HAlign align,
                                char paddingChar)
Converts the specified String into a fixed width string. Strings lesser in size than the fixed width are padded or truncated as required by the specified alignment.

Parameters:
str - the target string
width - the fixed width
align - the alignment of the target string within the width
paddingChar - the character to pad the string (if necessary);
Returns:
the transformed string

ellipsis

public static String ellipsis(String str,
                              String ellipsis,
                              int maxlen)
Converts the specified String into a string with maxlen characters, using the specified ellipsis as the suffix to denote the missing characters.

Parameters:
str - the target string
ellipsis - the ellipsis suffix
width - the max length, including the ellipsis
Returns:
the transformed string

ellipsis

public static String ellipsis(String str,
                              int maxlen)
Converts the specified String into a string with maxlen characters, using ... as the suffix to denote the missing characters.

Parameters:
str - the target string
width - the max length, including the ellipsis ("...")
Returns:
the transformed string

remove

public static String remove(String target,
                            char[] chars)
Removes all occurences of specified characters from the specified string and returns the new resulting string.

Parameters:
target - the string to remove characters from
chars - an array of characters, each of which is to be removed

removeTrailingSlash

public static String removeTrailingSlash(String str)
Removes the last forward or backward slash from the specified string (if any) and returns the resulting String. Returns null if a null argument is specified. The trailing slash is a slash that is the last character of the specified string.


removeBeginningSlash

public static String removeBeginningSlash(String str)
Removes the starting (at the very beginning of the string) forward or backward slash from the specified string (if any) and returns the resulting String. Returns null if null argument is specified.


removeSuffix

public static String removeSuffix(String name)
Removes the any file extension (.foo for example) from the specified name and returns the resulting String. Returns null if the specified path was null. This method takes a String as a parameter, as opposed to a java.io.File because the caller knows best what portion of the filename should be modified and returned by this method (full path name, name without path information etc).

Parameters:
name - the String denoting the file name to remove the extension from

dirName

public static String dirName(String str)
Returns the path component of the specified filename. If no path exists or the specified string is null, returns the empty string "". The path separator in the specified string should be /. If on a platform where this is not the case, the specified string should be modified to contain "/" before invoking this method. The returned pathName does contain the trailing "/". This ensures that dirName(somepath) + fileName(somepath) will always be equal to somepath.

The functionality of this method is different than java.io.File.getName()
and getParent(). Also unix dirname, basename are also compared below.

//Using java.io.File (getPath() returns the entire name, identical to
//the input, so is not shown. Sample run on windows:
Name=''         ; getName()='';       getParent()='null'
Name='/'        ; getName()='';       getParent()='null'
Name='/a'       ; getName()='a';      getParent()='\'
Name='a/b'      ; getName()='b';      getParent()='a'
Name='a/b.txt'  ; getName()='b.txt';  getParent()='a'
Name='b.txt'    ; getName()='b.txt';  getParent()='null'

Name='/a/'      ; getName()='a';      getParent()='\'
Name='/a/b/'    ; getName()='b';      getParent()='\a'
Name='a/b/'     ; getName()='b';      getParent()='a'
----------------------------
//Using these methods:
Name=''         ; fileName()='';      dirName()=''
Name='/'        ; fileName()='';      dirName()='/' 
Name='/a'       ; fileName()='a';     dirName()='/' 
Name='a/b'      ; fileName()='b';     dirName()='a/' 
Name='a/b.txt'  ; fileName()='b.txt'; dirName()='a/' 
Name='b.txt'    ; fileName()='b.txt'; dirName()='' 

Name='/a/'      ; fileName()='';      dirName()='/a/'
Name='/a/b/'    ; fileName()='';      dirName()='/a/b/'
Name='a/b/'     ; fileName()='';      dirName()='a/b/'
-----------------------------
//unix basename, dirname
Name=''         ; basename()='';    dirname()=''
Name='/'        ; basename()='/';   dirname()='/' 
Name='/a'       ; basename()='a';     dirname()='/' 
Name='a/b'      ; basename()='b';     dirname()='a/' 
Name='a/b.txt'  ; basename()='b.txt'; dirname()='a/' 
Name='b.txt'    ; basename()='b.txt'; dirname()='.' 

Name='/a/'      ; basename()='a';     dirname()='/'
Name='a/b/'     ; basename()='b';     dirname()='a'
Name='/a/b/'  ; fileName()='b';     dirName()='a'

-----------------------------
Note, the main differences among the 3 approaches above are in the last 2 statements in each section.


fileName

public static String fileName(String str)
Returns the file component of specified filename. If no filename exists or the specified string is null, returns the empty string "". The path separator in the specified string is always assumed to be /. If on a platform where this is not the case, the specified string should be modified to contain "/" before invoking this method. The returned file name does not contain the preceding "/".


split

public static List split(String str,
                         String delim)
Splits the string using the specified delimiters and returns the splits parts in a List. Use String.split(java.lang.String, int) instead for greater options.

Parameters:
str - the string to be tokenized
delim - delimiter string, each character in the string will be used as a delimiter to use while tokenizing

join

public static String join(List list,
                          String delim)
Joins the elements of the specified list, delimited by the specified delimiter.

Parameters:
list - containing the elements to be joined.
delim - delimits each element from the next

capitalWord

public static String capitalWord(String str)
Converts the specified String to start with a capital letter. Only the first character is made uppercase, the rest of the specified string is not affected.


sentenceCase

public static String sentenceCase(String str,
                                  String delimiters)
Converts the specified String to be in sentence case, whereby the first letter of each word in the sentence is uppercased and all other letters are lowercased. The characters in the delimiter string are used to delimit words in the sentence. If the delimiter string is null, the original string is returned as-is.
A runtime exception will be thrown if the specified string was null.


viewableAscii

public static String viewableAscii(String str)
Converts non printable ascii characters in the specified String to escaped or readable equivalents. For example, a newline is converted to the sequence \n and say, ascii character 29 is converted to the sequence of chars: 'ascii(29)'.

If the specified String is null, this method returns null.

Parameters:
str - the String to convert
Returns:
the converted String

viewableAscii

public static String viewableAscii(char c)
A version of viewableAscii(String) that takes a single char as a parameter.

Parameters:
char - the char to convert
Returns:
the ascii readable char

arrayToString

public static String arrayToString(char[] array)
Converts a character array into a viewable comma delimited string. Non-printable/readable characters are shown as readable equivalents.


listToString

public static String listToString(List list,
                                  String delim)
Converts a list into a string, each item being seperated by the specified delimiter. Each item in the list is converted by invokign toString on that item so the specified delimiter only applies to the outermost level.

The converted string does not start or end with any characters. To specify the start/end, use listToString(List, String, String, String)


listToString

public static String listToString(List list,
                                  String delim,
                                  String start,
                                  String end)
Converts a list into a string, each item being seperated by the specified delimiter. Each item in the list is converted by invokign toString on that item so the specified delimiter only applies to the outermost level.

The converted string start and ends with the specified chars as well.


escapeSingleQuotes

public static String escapeSingleQuotes(String str)
Escapes all single quotes in the specified a string with a backslash character.


escapeSingleQuotes

public static String escapeSingleQuotes(String str,
                                        String escape)
Escapes all single quotes in the specified a string with the specified escape character. So, if the specified escape character is ', all occurrences of o'tis the ele'phant becomes o''tis the ele''phant


escapeDoubleQuotes

public static String escapeDoubleQuotes(String str)
Escapes all double quotes in the specified a string with a backslash character.


escapeDoubleQuotes

public static String escapeDoubleQuotes(String str,
                                        String escape)
Escapes all double quotes in the specified a string with the specified escape character. So, if the specified escape character is \, all occurrences of o"tis the ele"phant becomes o\"tis the ele\"phant


main

public static void main(String[] args)