public class StringUtils extends Object
Modifier and Type | Method and Description |
---|---|
static List<Matcher> |
allMatches(String str,
String regex) |
static Map<String,String[]> |
argsToMap(String[] args)
Parses command line arguments into a Map.
|
static Map<String,String[]> |
argsToMap(String[] args,
Map<String,Integer> flagsToNumArgs)
Parses command line arguments into a Map.
|
static Properties |
argsToProperties(String[] args) |
static Properties |
argsToProperties(String[] args,
Map flagsToNumArgs)
Analagous to
argsToMap(java.lang.String[]) . |
static String |
capitalize(String s)
Uppercases the first character of a string.
|
static int |
editDistance(String s,
String t)
Computes the Levenshtein (edit) distance of the two given Strings.
|
static String |
escapeString(String s,
char[] charsToEscape,
char escapeChar) |
static String |
fileNameClean(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(String str,
String regex)
Say whether this regular expression can be found inside
this String.
|
static String |
join(Iterable l,
String glue)
Joins each elem in the Collection with the given glue.
|
static String |
join(List l)
Joins elems with a space.
|
static String |
join(List<?> l,
String glue)
Joins each elem in the List with the given glue.
|
static String |
join(Object[] elements)
Joins elems with a space.
|
static String |
join(Object[] elements,
String glue)
Joins each elem in the array with the given glue.
|
static int |
longestCommonSubstring(String s,
String t)
Computes the longest common substring of s and t.
|
static boolean |
lookingAt(String str,
String regex)
Say whether this regular expression can be found at the beginning of
this String.
|
static void |
main(String[] args) |
static boolean |
matches(String str,
String regex)
Say whether this regular expression matches
this String.
|
static int |
nthIndex(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 String |
pad(Object obj,
int totalChars)
Pads the toString value of the given Object.
|
static String |
pad(String str,
int totalChars)
Return a String of length a minimum of totalChars characters by
padding the input String str with spaces.
|
static String |
padLeft(double d,
int totalChars) |
static String |
padLeft(int i,
int totalChars) |
static String |
padLeft(Object obj,
int totalChars) |
static String |
padLeft(String str,
int totalChars)
Pads the given String to the left with spaces to ensure that it's
at least totalChars long.
|
static String |
padOrTrim(Object obj,
int totalChars)
Pad or trim the toString value of the given Object.
|
static String |
padOrTrim(String str,
int num)
Pad or trim so as to produce a string of exactly a certain length.
|
static Map |
parseCommandLineArguments(String[] args)
A simpler form of command line argument parsing.
|
static String |
pennPOSToWordnetPOS(String s)
Computes the WordNet 2.0 POS tag corresponding to the PTB POS tag s.
|
static void |
printStringOneCharPerLine(String s) |
static void |
printToFile(File file,
String message)
Prints to a file.
|
static void |
printToFile(File file,
String message,
boolean append)
Prints to a file.
|
static void |
printToFile(String filename,
String message)
Prints to a file.
|
static void |
printToFile(String filename,
String message,
boolean append)
Prints to a file.
|
static String |
slurpFile(File file)
Returns all the text in the given File.
|
static String |
slurpFile(String filename)
Returns all the text in the given file
|
static String |
slurpFile(String filename,
String encoding)
Returns all the text in the given file with the given encoding.
|
static String |
slurpFileNoExceptions(File file)
Returns all the text in the given File.
|
static String |
slurpFileNoExceptions(String filename)
Returns all the text in the given File.
|
static String |
slurpFileNoExceptions(String filename,
String encoding)
Returns all the text in the given file with the given encoding.
|
static String |
slurpGBFile(String filename) |
static String |
slurpGBFileNoExceptions(String filename) |
static String |
slurpGBURL(URL u)
Returns all the text at the given URL.
|
static String |
slurpGBURLNoExceptions(URL u)
Returns all the text at the given URL.
|
static String |
slurpReader(Reader reader)
Returns all the text from the given Reader.
|
static String |
slurpURL(String path)
Returns all the text at the given URL.
|
static String |
slurpURL(URL u)
Returns all the text at the given URL.
|
static String |
slurpURL(URL u,
String encoding)
Returns all the text at the given URL.
|
static String |
slurpURLNoExceptions(String path)
Returns all the text at the given URL.
|
static String |
slurpURLNoExceptions(URL u)
Returns all the text at the given URL.
|
static String |
slurpURLNoExceptions(URL u,
String encoding)
Returns all the text at the given URL.
|
static List |
split(String s)
Splits on whitespace (\\s+).
|
static List |
split(String str,
String regex)
Splits the given string using the given regex as delimiters.
|
static String[] |
splitOnCharWithQuoting(String s,
char splitChar,
char quoteChar,
char escapeChar)
This function splits the String s into multiple Strings using the
splitChar.
|
static Properties |
stringToProperties(String str)
This method converts a comma-separated String (with whitespace
optionally allowed after the comma) representing properties
to a Properties object.
|
static String |
stripNonAlphaNumerics(String orig) |
static String |
trim(Object obj,
int maxWidth) |
static String |
trim(String s,
int maxWidth)
Returns s if it's at most maxWidth chars, otherwise chops right side to fit.
|
static 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(String str, String regex)
str
- String to search for match inregex
- String to compile as the regular expressionpublic static boolean lookingAt(String str, String regex)
str
- String to search for match at start ofregex
- String to compile as the regular expressionpublic static boolean matches(String str, String regex)
str
- String to search for match at start ofregex
- String to compile as the regular expressionpublic static String slurpFile(File file) throws IOException
IOException
public static String slurpFile(String filename, String encoding) throws IOException
IOException
public static String slurpFileNoExceptions(String filename, String encoding)
null
.public static String slurpGBFile(String filename) throws IOException
IOException
public static String slurpReader(Reader reader)
public static String slurpFile(String filename) throws IOException
IOException
public static String slurpFileNoExceptions(File file)
null
.public static String slurpFileNoExceptions(String filename)
null
.public static String slurpGBURL(URL u) throws IOException
IOException
public static String slurpGBURLNoExceptions(URL u)
public static String slurpURLNoExceptions(URL u, String encoding)
public static String slurpURL(URL u, String encoding) throws IOException
IOException
public static String slurpURL(URL u) throws IOException
IOException
public static String slurpURLNoExceptions(URL u)
public static String slurpURL(String path) throws Exception
Exception
public static String slurpURLNoExceptions(String path)
null
.public static String join(Iterable l, String glue)
public static String join(List<?> l, String glue)
public static String join(Object[] elements, String glue)
public static List split(String str, String regex)
str
- String to split upregex
- String to compile as the regular expressionpublic static String pad(String str, int totalChars)
public static String pad(Object obj, int totalChars)
public static String padOrTrim(String str, int num)
str
- The String to be padded or truncatednum
- The desired lengthpublic static String padOrTrim(Object obj, int totalChars)
public static String padLeft(String str, int totalChars)
public static String padLeft(int i, int totalChars)
public static String padLeft(double d, int totalChars)
public static String trim(String s, int maxWidth)
public static String fileNameClean(String s)
public static int nthIndex(String s, char ch, int n)
public static String truncate(int n, int smallestDigit, int biggestDigit)
public static Map<String,String[]> argsToMap(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 Map<String,String[]> argsToMap(String[] args, Map<String,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);
public static Properties argsToProperties(String[] args)
public static Properties argsToProperties(String[] args, Map flagsToNumArgs)
argsToMap(java.lang.String[])
. However, there are several key differences between this method and argsToMap(java.lang.String[])
:
public static Properties stringToProperties(String str)
public static void printToFile(File file, String message, boolean append)
append=true
, and overwrites if append=false
public static void printToFile(File file, String message)
public static void printToFile(String filename, String message, boolean append)
append=true
, and overwrites if append=false
public static void printToFile(String filename, String message)
public static Map parseCommandLineArguments(String[] args)
args
- public static void printStringOneCharPerLine(String s)
public static String[] splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)
s
- The String to splitsplitChar
- quoteChar
- public static int longestCommonSubstring(String s, String t)
public static int editDistance(String s, String t)
public static String pennPOSToWordnetPOS(String s)
s
- a Penn TreeBank POS tag.public static String capitalize(String s)
s
- a string to capitalizepublic static void main(String[] args) throws IOException
IOException
Copyright © 2016. All Rights Reserved.