public class StringUtils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static java.util.List<java.util.regex.Matcher> |
allMatches(java.lang.String str,
java.lang.String regex) |
static java.util.Map<java.lang.String,java.lang.String[]> |
argsToMap(java.lang.String[] args)
Parses command line arguments into a Map.
|
static java.util.Map<java.lang.String,java.lang.String[]> |
argsToMap(java.lang.String[] args,
java.util.Map<java.lang.String,java.lang.Integer> flagsToNumArgs)
Parses command line arguments into a Map.
|
static java.util.Properties |
argsToProperties(java.lang.String[] args) |
static java.util.Properties |
argsToProperties(java.lang.String[] args,
java.util.Map flagsToNumArgs)
Analagous to
argsToMap(java.lang.String[]) . |
static java.lang.String |
capitalize(java.lang.String s)
Uppercases the first character of a string.
|
static int |
editDistance(java.lang.String s,
java.lang.String t)
Computes the Levenshtein (edit) distance of the two given Strings.
|
static java.lang.String |
escapeString(java.lang.String s,
char[] charsToEscape,
char escapeChar) |
static java.lang.String |
fileNameClean(java.lang.String s)
Returns a "clean" version of the given filename in which spaces have
been converted to dashes and all non-alphaneumeric chars are underscores.
|
static boolean |
find(java.lang.String str,
java.lang.String regex)
Say whether this regular expression can be found inside
this String.
|
static java.lang.String |
join(java.lang.Iterable l,
java.lang.String glue)
Joins each elem in the Collection with the given glue.
|
static java.lang.String |
join(java.util.List l)
Joins elems with a space.
|
static java.lang.String |
join(java.util.List<?> l,
java.lang.String glue)
Joins each elem in the List with the given glue.
|
static java.lang.String |
join(java.lang.Object[] elements)
Joins elems with a space.
|
static java.lang.String |
join(java.lang.Object[] elements,
java.lang.String glue)
Joins each elem in the array with the given glue.
|
static int |
longestCommonSubstring(java.lang.String s,
java.lang.String t)
Computes the longest common substring of s and t.
|
static boolean |
lookingAt(java.lang.String str,
java.lang.String regex)
Say whether this regular expression can be found at the beginning of
this String.
|
static void |
main(java.lang.String[] args) |
static boolean |
matches(java.lang.String str,
java.lang.String regex)
Say whether this regular expression matches
this String.
|
static int |
nthIndex(java.lang.String s,
char ch,
int n)
Returns the index of the nth occurrence of ch in s, or -1
if there are less than n occurrences of ch.
|
static java.lang.String |
pad(java.lang.Object obj,
int totalChars)
Pads the toString value of the given Object.
|
static java.lang.String |
pad(java.lang.String str,
int totalChars)
Return a String of length a minimum of totalChars characters by
padding the input String str with spaces.
|
static java.lang.String |
padLeft(double d,
int totalChars) |
static java.lang.String |
padLeft(int i,
int totalChars) |
static java.lang.String |
padLeft(java.lang.Object obj,
int totalChars) |
static java.lang.String |
padLeft(java.lang.String str,
int totalChars)
Pads the given String to the left with spaces to ensure that it's
at least totalChars long.
|
static java.lang.String |
padOrTrim(java.lang.Object obj,
int totalChars)
Pad or trim the toString value of the given Object.
|
static java.lang.String |
padOrTrim(java.lang.String str,
int num)
Pad or trim so as to produce a string of exactly a certain length.
|
static java.util.Map |
parseCommandLineArguments(java.lang.String[] args)
A simpler form of command line argument parsing.
|
static java.lang.String |
pennPOSToWordnetPOS(java.lang.String s)
Computes the WordNet 2.0 POS tag corresponding to the PTB POS tag s.
|
static void |
printStringOneCharPerLine(java.lang.String s) |
static void |
printToFile(java.io.File file,
java.lang.String message)
Prints to a file.
|
static void |
printToFile(java.io.File file,
java.lang.String message,
boolean append)
Prints to a file.
|
static void |
printToFile(java.lang.String filename,
java.lang.String message)
Prints to a file.
|
static void |
printToFile(java.lang.String filename,
java.lang.String message,
boolean append)
Prints to a file.
|
static java.lang.String |
slurpFile(java.io.File file)
Returns all the text in the given File.
|
static java.lang.String |
slurpFile(java.lang.String filename)
Returns all the text in the given file
|
static java.lang.String |
slurpFile(java.lang.String filename,
java.lang.String encoding)
Returns all the text in the given file with the given encoding.
|
static java.lang.String |
slurpFileNoExceptions(java.io.File file)
Returns all the text in the given File.
|
static java.lang.String |
slurpFileNoExceptions(java.lang.String filename)
Returns all the text in the given File.
|
static java.lang.String |
slurpFileNoExceptions(java.lang.String filename,
java.lang.String encoding)
Returns all the text in the given file with the given encoding.
|
static java.lang.String |
slurpGBFile(java.lang.String filename) |
static java.lang.String |
slurpGBFileNoExceptions(java.lang.String filename) |
static java.lang.String |
slurpGBURL(java.net.URL u)
Returns all the text at the given URL.
|
static java.lang.String |
slurpGBURLNoExceptions(java.net.URL u)
Returns all the text at the given URL.
|
static java.lang.String |
slurpReader(java.io.Reader reader)
Returns all the text from the given Reader.
|
static java.lang.String |
slurpURL(java.lang.String path)
Returns all the text at the given URL.
|
static java.lang.String |
slurpURL(java.net.URL u)
Returns all the text at the given URL.
|
static java.lang.String |
slurpURL(java.net.URL u,
java.lang.String encoding)
Returns all the text at the given URL.
|
static java.lang.String |
slurpURLNoExceptions(java.lang.String path)
Returns all the text at the given URL.
|
static java.lang.String |
slurpURLNoExceptions(java.net.URL u)
Returns all the text at the given URL.
|
static java.lang.String |
slurpURLNoExceptions(java.net.URL u,
java.lang.String encoding)
Returns all the text at the given URL.
|
static java.util.List |
split(java.lang.String s)
Splits on whitespace (\\s+).
|
static java.util.List |
split(java.lang.String str,
java.lang.String regex)
Splits the given string using the given regex as delimiters.
|
static java.lang.String[] |
splitOnCharWithQuoting(java.lang.String s,
char splitChar,
char quoteChar,
char escapeChar)
This function splits the String s into multiple Strings using the
splitChar.
|
static java.util.Properties |
stringToProperties(java.lang.String str)
This method converts a comma-separated String (with whitespace
optionally allowed after the comma) representing properties
to a Properties object.
|
static java.lang.String |
stripNonAlphaNumerics(java.lang.String orig) |
static java.lang.String |
trim(java.lang.Object obj,
int maxWidth) |
static java.lang.String |
trim(java.lang.String s,
int maxWidth)
Returns s if it's at most maxWidth chars, otherwise chops right side to fit.
|
static java.lang.String |
truncate(int n,
int smallestDigit,
int biggestDigit)
This returns a string from decimal digit smallestDigit to decimal digit
biggest digit.
|
public static boolean find(java.lang.String str, java.lang.String regex)
str
- String to search for match inregex
- String to compile as the regular expressionpublic static boolean lookingAt(java.lang.String str, java.lang.String regex)
str
- String to search for match at start ofregex
- String to compile as the regular expressionpublic static boolean matches(java.lang.String str, java.lang.String regex)
str
- String to search for match at start ofregex
- String to compile as the regular expressionpublic static java.lang.String slurpFile(java.io.File file) throws java.io.IOException
java.io.IOException
public static java.lang.String slurpGBFileNoExceptions(java.lang.String filename)
public static java.lang.String slurpFile(java.lang.String filename, java.lang.String encoding) throws java.io.IOException
java.io.IOException
public static java.lang.String slurpFileNoExceptions(java.lang.String filename, java.lang.String encoding)
null
.public static java.lang.String slurpGBFile(java.lang.String filename) throws java.io.IOException
java.io.IOException
public static java.lang.String slurpReader(java.io.Reader reader)
public static java.lang.String slurpFile(java.lang.String filename) throws java.io.IOException
java.io.IOException
public static java.lang.String slurpFileNoExceptions(java.io.File file)
null
.public static java.lang.String slurpFileNoExceptions(java.lang.String filename)
null
.public static java.lang.String slurpGBURL(java.net.URL u) throws java.io.IOException
java.io.IOException
public static java.lang.String slurpGBURLNoExceptions(java.net.URL u)
public static java.lang.String slurpURLNoExceptions(java.net.URL u, java.lang.String encoding)
public static java.lang.String slurpURL(java.net.URL u, java.lang.String encoding) throws java.io.IOException
java.io.IOException
public static java.lang.String slurpURL(java.net.URL u) throws java.io.IOException
java.io.IOException
public static java.lang.String slurpURLNoExceptions(java.net.URL u)
public static java.lang.String slurpURL(java.lang.String path) throws java.lang.Exception
java.lang.Exception
public static java.lang.String slurpURLNoExceptions(java.lang.String path)
null
.public static java.lang.String join(java.lang.Iterable l, java.lang.String glue)
public static java.lang.String join(java.util.List<?> l, java.lang.String glue)
public static java.lang.String join(java.lang.Object[] elements, java.lang.String glue)
public static java.lang.String join(java.util.List l)
public static java.lang.String join(java.lang.Object[] elements)
public static java.util.List split(java.lang.String s)
public static java.util.List split(java.lang.String str, java.lang.String regex)
str
- String to split upregex
- String to compile as the regular expressionpublic static java.lang.String pad(java.lang.String str, int totalChars)
public static java.lang.String pad(java.lang.Object obj, int totalChars)
public static java.lang.String padOrTrim(java.lang.String str, int num)
str
- The String to be padded or truncatednum
- The desired lengthpublic static java.lang.String padOrTrim(java.lang.Object obj, int totalChars)
public static java.lang.String padLeft(java.lang.String str, int totalChars)
public static java.lang.String padLeft(java.lang.Object obj, int totalChars)
public static java.lang.String padLeft(int i, int totalChars)
public static java.lang.String padLeft(double d, int totalChars)
public static java.lang.String trim(java.lang.String s, int maxWidth)
public static java.lang.String trim(java.lang.Object obj, int maxWidth)
public static java.lang.String fileNameClean(java.lang.String s)
public static int nthIndex(java.lang.String s, char ch, int n)
public static java.lang.String truncate(int n, int smallestDigit, int biggestDigit)
public static java.util.Map<java.lang.String,java.lang.String[]> argsToMap(java.lang.String[] args)
String[]
containing
the optional arguments (if present). The non-flag values not
captured as flag arguments are collected into a String[] array
and returned as the value of null
in the Map. In
this invocation, flags cannot take arguments, so all the String
array values other than the value for null
will be zero-length.args
- Map
of flag names to flag argument String[]
arrays.public static java.util.Map<java.lang.String,java.lang.String[]> argsToMap(java.lang.String[] args, java.util.Map<java.lang.String,java.lang.Integer> flagsToNumArgs)
String[]
containing
the optional arguments (if present). The non-flag values not
captured as flag arguments are collected into a String[] array
and returned as the value of null
in the Map. In
this invocation, the maximum number of arguments for each flag
can be specified as an Integer
value of the appropriate
flag key in the flagsToNumArgs
Map
argument. (By default, flags cannot take arguments.)
Example of usage:
Map flagsToNumArgs = new HashMap();
flagsToNumArgs.put("-x",new Integer(2));
flagsToNumArgs.put("-d",new Integer(1));
Map result = argsToMap(args,flagsToNumArgs);
args
- the argument array to be parsedflagsToNumArgs
- a Map
of flag names to Integer
values specifying the maximum number of allowed
arguments for that flag (default 0).Map
of flag names to flag argument String[]
arrays.public static java.util.Properties argsToProperties(java.lang.String[] args)
public static java.util.Properties argsToProperties(java.lang.String[] args, java.util.Map flagsToNumArgs)
argsToMap(java.lang.String[])
. However, there are several key differences between this method and argsToMap(java.lang.String[])
:
public static java.util.Properties stringToProperties(java.lang.String str)
public static void printToFile(java.io.File file, java.lang.String message, boolean append)
append=true
, and overwrites if append=false
public static void printToFile(java.io.File file, java.lang.String message)
public static void printToFile(java.lang.String filename, java.lang.String message, boolean append)
append=true
, and overwrites if append=false
public static void printToFile(java.lang.String filename, java.lang.String message)
public static java.util.Map parseCommandLineArguments(java.lang.String[] args)
args
- public static java.lang.String stripNonAlphaNumerics(java.lang.String orig)
public static void printStringOneCharPerLine(java.lang.String s)
public static java.lang.String escapeString(java.lang.String s, char[] charsToEscape, char escapeChar)
public static java.lang.String[] splitOnCharWithQuoting(java.lang.String s, char splitChar, char quoteChar, char escapeChar)
s
- The String to splitsplitChar
- quoteChar
- public static int longestCommonSubstring(java.lang.String s, java.lang.String t)
public static int editDistance(java.lang.String s, java.lang.String t)
public static java.lang.String pennPOSToWordnetPOS(java.lang.String s)
s
- a Penn TreeBank POS tag.public static java.lang.String capitalize(java.lang.String s)
s
- a string to capitalizepublic static java.util.List<java.util.regex.Matcher> allMatches(java.lang.String str, java.lang.String regex)
public static void main(java.lang.String[] args) throws java.io.IOException
java.io.IOException