org.tridas.io.util
Class StringUtils

java.lang.Object
  extended by org.tridas.io.util.StringUtils

public class StringUtils
extends Object

Some handy string utilities.

Left to do

Note from daniel--splitBy looks awfully replacable by String.split

Version:
$Id: StringUtils.java 1015 2008-02-19 21:11:33Z lucasm $
Author:
Ken Harris <kbh7 at cornell dot edu>

Method Summary
static String addLefthandZeros(int argNum, int argNumCharacters)
          by daniel, for integer formatting
static String bytesToHex(byte[] in)
           
static String[] chopString(String argString, int argLength)
          Chops the string into smaller strings with size of argLength.
static String escapeForCSV(String input)
          escapeForCSV given a string, replace any occurances of " with double "'s, and return it surrounded in "'s
static String escapeForXML(String input)
          Given a string, escape any < > & ' " characters for XML.
static int[] extractInts(String s)
          Convert a sequence of integers in a string into an array of ints.
static String getIntialsFromName(String name, Integer charLimitGuide)
          Takes a full name and returns the initials.
static String getSpaces(int num)
          Get a specified number of species
static Boolean isStringWholeInteger(String str)
          Returns true if string can be interpreted as a whole number integer.
static String leftPad(String text, int size)
          Pad some text on the left (i.e., right-align it) until it's a specified width.
static String parseInitials(String fullName)
          Returns upper case initials from a persons name.
static String rightPad(String text, int size)
           
static String rightPadWithTrim(String text, int size)
          Returns a string of a specified length.
static String[] splitBy(String text, char sep)
          Split some text, using an arbitrary separator character.
static String[] splitByLines(String text)
          Split some text into lines.
static String substitute(String str, String source, String target)
          In a string, replace one substring with another.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

leftPad

public static String leftPad(String text,
                             int size)
Pad some text on the left (i.e., right-align it) until it's a specified width. If the text is already longer than the desired length, it is returned.

Parameters:
text - string to pad
size - length of resulting string
Returns:
the original text, padded on the left

rightPadWithTrim

public static String rightPadWithTrim(String text,
                                      int size)
Returns a string of a specified length. If supplied text is longer than size then the string is truncated and returned. Otherwise it returns the string padded with spaces.

Parameters:
text - string to pad
size - length of resulting string
Returns:
the original text, padded on the right if required

rightPad

public static String rightPad(String text,
                              int size)

addLefthandZeros

public static String addLefthandZeros(int argNum,
                                      int argNumCharacters)
by daniel, for integer formatting

Parameters:
argNum -
argNumCharacters -
Returns:

chopString

public static String[] chopString(String argString,
                                  int argLength)
Chops the string into smaller strings with size of argLength. by daniel

Parameters:
argString -
argLength -
Returns:

splitByLines

public static String[] splitByLines(String text)
Split some text into lines. For example, "a\nb\nc" becomes String[] {"a","b","c"}.

Parameters:
text - the text, separated by '\n' chars
Returns:
the string, as an array of strings

splitBy

public static String[] splitBy(String text,
                               char sep)
Split some text, using an arbitrary separator character.

Parameters:
text - the text
sep - the separator character to watch for
Returns:
the string, as an array of strings

getSpaces

public static String getSpaces(int num)
Get a specified number of species

Parameters:
num -
Returns:

extractInts

public static int[] extractInts(String s)
Convert a sequence of integers in a string into an array of ints.

For example, extractInts("1 2 3") = int[] { 1, 2, 3 }.

Bug: what happens if the string isn't parseable?

Parameters:
s - the string to parse
Returns:
an array of ints, representing the string

escapeForXML

public static String escapeForXML(String input)
Given a string, escape any < > & ' " characters for XML. Also, if any characters are unprintable, they're escaped as raw values (&#xxxx;), so loading the output in any old text editor shouldn't mangle anything.

Parameters:
input - a string
Returns:
the same string, with </>/& escaped

escapeForCSV

public static String escapeForCSV(String input)
escapeForCSV given a string, replace any occurances of " with double "'s, and return it surrounded in "'s

Parameters:
input - a string
Returns:
the same string, with "'s escaped

substitute

public static String substitute(String str,
                                String source,
                                String target)
In a string, replace one substring with another.

If str contains source, return a new string with (the first occurrence of) source replaced by target.

If str doesn't contain source (or str is the empty string), returns str.

Think: str ~= s/source/target/

This is like Java 1.4's String.replaceFirst() method; when I decide to drop support for Java 1.3, I can use that method instead.

Parameters:
str - the base string
source - the substring to look for
target - the replacement string to use
Returns:
the original string, str, with its first instance of source replaced by target

bytesToHex

public static String bytesToHex(byte[] in)

parseInitials

public static String parseInitials(String fullName)
Returns upper case initials from a persons name. Expects fullName to be in the format 'Firstname Surname' or 'Surname, Firstname'. If fullName is in a different format then it simply returns the first two characters with case unchanged.

Parameters:
fullName -
Returns:

isStringWholeInteger

public static Boolean isStringWholeInteger(String str)
Returns true if string can be interpreted as a whole number integer. If string includes a decimal place it will still return true as long as the decimal portion is zero. "130" = TRUE "130.0" = TRUE "130.4" = FALSE "blah" = FALSE

Parameters:
str -
Returns:

getIntialsFromName

public static String getIntialsFromName(String name,
                                        Integer charLimitGuide)
Takes a full name and returns the initials. If name contains a comma then it is treated as "Lastname, Firstname". If name is equal or shorter than charLimit then it is returned as is.

Parameters:
name -
charLimitGuide - = 2
Returns:


Copyright © 2011. All Rights Reserved.