org.openflow.util.string
Class StringOps

java.lang.Object
  extended by org.openflow.util.string.StringOps

public abstract class StringOps
extends java.lang.Object

Defines some methods for matching and replacement on special conditions for Strings.

Author:
David Underhill

Constructor Summary
StringOps()
           
 
Method Summary
static java.lang.String checkName(java.lang.String name)
          Checks to see if name contains anything other than alphanumeric characters, underscores, or spaces.
static java.lang.String endWithSlash(java.lang.String s)
          Make sure the last character in the specified String is a slash unless s is of length 0 in which case the empty string passed in is returned.
static java.lang.String formatBits(long num_bits, boolean toBytes)
           
static java.lang.String formatBitsPerSec(float util, long max_rate)
           
static java.lang.String formatBitsPerSec(long num_bits)
           
static java.lang.String formatBitsPerSec(long num_bits, boolean toBytes)
           
static java.lang.String formatSecs(int num_msecs)
           
static java.lang.String getFileAsString(java.lang.String fn)
          Gets the contents of a file as a String
static java.lang.String getFileTitle(java.lang.String s)
          Gets the text following the last slash (\ or /) in a string (the "file title")
static java.lang.String getPath(java.lang.String fn)
          determine the path in the string (all text before and included the last slash mark, or the empty string if there are no slashes)
static int indexOf(java.lang.String s, java.lang.String searchTerm)
          finds the searchTerm in String s from startIndex which doesn't have a letter, digit, or underscore on either side of it
static int indexOf(java.lang.String s, java.lang.String searchTerm, int startIndex)
          finds the searchTerm in String s from startIndex which doesn't have a letter, digit, or underscore on either side of it
static java.lang.String replace(java.lang.String s, java.lang.String searchTerm, java.lang.String replaceTerm)
          replaces all occurs of searchTerm in s which don't have a letter, digit, or underscore on either side
static java.lang.String replaceBetweenStrings(java.lang.String str, java.lang.String s1, java.lang.String s2, java.lang.String repl)
          Replaces the text between two strings (and removes the specified strings themselves too)
static java.lang.String selectBetweenStrings(java.lang.String str, java.lang.String s1, java.lang.String s2)
          Selects the text between two strings
static java.lang.String[] splitArgs(java.lang.String args)
          Breaks the args string into an array of strings where each element is an individual argument.
static java.lang.String splitIntoLines(java.lang.String s)
          inserts a newline after every 80 characters
static java.lang.String splitIntoLines(java.lang.String s, int maxLineLen)
          inserts a newline after every maxLineLen characters
static java.lang.String splitIntoLines(java.lang.String s, int firstLineMaxLen, int maxLineLen, java.lang.String newlineIdentifier, boolean trim)
          inserts a newline string specified by the user after every maxLineLen characters
static java.lang.String splitIntoLines(java.lang.String s, int maxLineLen, java.lang.String newlineIdentifier, boolean trim)
          inserts a newline string specified by the user after every maxLineLen characters
static java.lang.String stripExtensions(java.lang.String s, int numExtToStrip)
          Removes a specified number of extensions from s
static void writeStringToFile(java.lang.String fn, java.lang.String str)
          Writes a string to a file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringOps

public StringOps()
Method Detail

formatBits

public static java.lang.String formatBits(long num_bits,
                                          boolean toBytes)

formatBitsPerSec

public static java.lang.String formatBitsPerSec(long num_bits,
                                                boolean toBytes)

formatBitsPerSec

public static java.lang.String formatBitsPerSec(long num_bits)

formatBitsPerSec

public static java.lang.String formatBitsPerSec(float util,
                                                long max_rate)

formatSecs

public static java.lang.String formatSecs(int num_msecs)

checkName

public static java.lang.String checkName(java.lang.String name)
                                  throws IllegalArgValException
Checks to see if name contains anything other than alphanumeric characters, underscores, or spaces.

Parameters:
name - the string to check
Returns:
name if it is valid.
Throws:
IllegalArgValException - occurs if the name is invalid.

endWithSlash

public static java.lang.String endWithSlash(java.lang.String s)
Make sure the last character in the specified String is a slash unless s is of length 0 in which case the empty string passed in is returned.

Parameters:
s - the string to check
Returns:
s concatenated with a `/` if it didn't already end with a slash (`\` or `/`)

getPath

public static java.lang.String getPath(java.lang.String fn)
determine the path in the string (all text before and included the last slash mark, or the empty string if there are no slashes)

Parameters:
fn - the filename to find the path in
Returns:
the path to the file specified in fn included the trailing slash as appropriate

getFileAsString

public static java.lang.String getFileAsString(java.lang.String fn)
                                        throws java.io.IOException
Gets the contents of a file as a String

Parameters:
fn - the file to read
Returns:
the contents of the file as a string
Throws:
java.io.IOException - occurs if the file is invalid or unable to be properly read

getFileTitle

public static java.lang.String getFileTitle(java.lang.String s)
Gets the text following the last slash (\ or /) in a string (the "file title")

Parameters:
s - the string to work with
Returns:
all text is s following the last slash, or s if there were no slashes, or s if s is length 1.

stripExtensions

public static java.lang.String stripExtensions(java.lang.String s,
                                               int numExtToStrip)
Removes a specified number of extensions from s

Parameters:
s - the string to remove extensions from (.ext)
numExtToStrip - the number of extensions to remove
Returns:
s without the specified number of extensions (if s has fewer extensions that numExtToStrip then all extensions are stripped from s). The return may be of zero length.

replace

public static java.lang.String replace(java.lang.String s,
                                       java.lang.String searchTerm,
                                       java.lang.String replaceTerm)
replaces all occurs of searchTerm in s which don't have a letter, digit, or underscore on either side

Parameters:
s - the string being searched
searchTerm - what is being searched for
replaceTerm - what searchTerm is being replaced with
Returns:
s with all occurrences of searchTerm replaced with replaceTerm

replaceBetweenStrings

public static java.lang.String replaceBetweenStrings(java.lang.String str,
                                                     java.lang.String s1,
                                                     java.lang.String s2,
                                                     java.lang.String repl)
Replaces the text between two strings (and removes the specified strings themselves too)

Parameters:
str - the string to search
s1 - the first string to find
s2 - the second string to find
repl - the string to use to replace the range from the beginning s1 to the end s2 inclusive
Returns:
the resulting string, or the string str which was passed in if s1 or s2 are not found

selectBetweenStrings

public static java.lang.String selectBetweenStrings(java.lang.String str,
                                                    java.lang.String s1,
                                                    java.lang.String s2)
Selects the text between two strings

Parameters:
str - the string to search
s1 - the first string to find
s2 - the second string to find
Returns:
the text between s1 and s2 (trimmed) or an empty string if either string isn't found

writeStringToFile

public static void writeStringToFile(java.lang.String fn,
                                     java.lang.String str)
                              throws java.io.IOException
Writes a string to a file

Parameters:
fn - the file to write the string to
str - the string to write
Throws:
java.io.IOException - occurs if the file is invalid or unable to be properly read

indexOf

public static int indexOf(java.lang.String s,
                          java.lang.String searchTerm)
finds the searchTerm in String s from startIndex which doesn't have a letter, digit, or underscore on either side of it

Parameters:
s - the string being searched
searchTerm - what is being searched for
Returns:
the index in s at which searchTerm occurs or -1 if it is not found

indexOf

public static int indexOf(java.lang.String s,
                          java.lang.String searchTerm,
                          int startIndex)
finds the searchTerm in String s from startIndex which doesn't have a letter, digit, or underscore on either side of it

Parameters:
s - the string being searched
searchTerm - what is being searched for
startIndex - where to start looking
Returns:
the index in s at which searchTerm occurs or -1 if it is not found

splitIntoLines

public static java.lang.String splitIntoLines(java.lang.String s)
inserts a newline after every 80 characters

Parameters:
s - the string to split
Returns:
the split up string

splitIntoLines

public static java.lang.String splitIntoLines(java.lang.String s,
                                              int maxLineLen)
inserts a newline after every maxLineLen characters

Parameters:
s - the string to split
maxLineLen - the max length (in characters) a line may be
Returns:
the split up string

splitIntoLines

public static java.lang.String splitIntoLines(java.lang.String s,
                                              int maxLineLen,
                                              java.lang.String newlineIdentifier,
                                              boolean trim)
inserts a newline string specified by the user after every maxLineLen characters

Parameters:
s - the string to split
maxLineLen - the max length (in characters) a line may be
newlineIdentifier - the string to use to split lines up
trim - whether or not trim whitespace of each line's ends
Returns:
the split up string

splitIntoLines

public static java.lang.String splitIntoLines(java.lang.String s,
                                              int firstLineMaxLen,
                                              int maxLineLen,
                                              java.lang.String newlineIdentifier,
                                              boolean trim)
inserts a newline string specified by the user after every maxLineLen characters

Parameters:
s - the string to split
firstLineMaxLen - the max length (in characters) the first line may be
maxLineLen - the max length (in characters) a line may be
newlineIdentifier - the string to use to split lines up
trim - whether or not trim whitespace of each line's ends
Returns:
the split up string

splitArgs

public static java.lang.String[] splitArgs(java.lang.String args)
Breaks the args string into an array of strings where each element is an individual argument. Arguments are separated by spaces, though spaces can be included by enclosing the argument in quote (") marks. A beginning quote mark must be preceded by a space. An ending quote mark must be followed by a space.

Parameters:
args - the arguments string to parse
Returns:
an array of individual arguments, or null if args was null or the empty string, or no args were parsed