public class StringUtils extends Object
Many of these methods will be familiar to perl users: join(Iterable)
, split(String, String)
, trim(String, int)
, find(String, String)
, lookingAt(String, String)
, and matches(String,
String)
.
There are also useful methods for padding Strings/Objects with spaces on the right or left for printing even-width
table columns: padLeft(int, int)
, pad(String, int)
.
Example: print a comma-separated list of numbers:
System.out.println(StringUtils.pad(nums, ", "));
Example: print a 2D array of numbers with 8-char cells:
for(int i = 0; i < nums.length; i++) {
for(int j = 0; j < nums[i].length; j++) {
System.out.print(StringUtils.leftPad(nums[i][j], 8));
System.out.println();
Modifier and Type | Field and Description |
---|---|
static String[] |
EMPTY_STRING_ARRAY |
Modifier and Type | Method and Description |
---|---|
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)
In this version each flag has zero or one argument.
|
static Properties |
argsToProperties(String[] args,
Map<String,Integer> flagsToNumArgs)
Analogous to
argsToMap(java.lang.String[]) . |
static Properties |
argsToPropertiesWithResolve(String[] args)
convert args to properties with variable names resolved.
|
static String |
capitalize(String s)
Uppercases the first character of a string.
|
static String |
checkRequiredProperties(Properties props,
String... requiredProps)
If any of the given list of properties are not found, returns the
name of that property.
|
static String |
chomp(Object o)
Returns the result of calling toString() on the supplied Object, but with
any trailing '\n' or '\r\n' removed.
|
static String |
chomp(String s)
Returns the supplied string with any trailing '\n' or '\r\n' removed.
|
static <T> T |
columnStringToObject(Class<?> objClass,
String str,
Pattern delimiterPattern,
String[] fieldNames)
Converts a tab delimited string into an object with given fields
Requires the object has public access for the specified fields
|
static <T> T |
columnStringToObject(Class objClass,
String str,
String delimiterRegex,
String[] fieldNames)
Converts a tab delimited string into an object with given fields
Requires the object has setXxx functions for the specified fields
|
static boolean |
containsIgnoreCase(Collection<String> c,
String s)
Convenience method: a case-insensitive variant of Collection.contains.
|
static String[] |
decodeArray(String encoded)
Decode an array encoded as a String.
|
static Map<String,String> |
decodeMap(String encoded)
Decode a map encoded as a string.
|
static int |
editDistance(String s,
String t)
Computes the Levenshtein (edit) distance of the two given Strings.
|
static String |
escapeJsonString(String str) |
static String |
escapeString(String s,
char[] charsToEscape,
char escapeChar) |
static String |
expandEnvironmentVariables(String raw)
Takes an input String, and replaces any bash-style variables (e.g., $VAR_NAME)
with its actual environment variable from
System.getenv() . |
static String |
expandEnvironmentVariables(String raw,
Map<String,String> env)
Takes an input String, and replaces any bash-style variables (e.g., $VAR_NAME)
with its actual environment variable from the passed environment specification.
|
static String |
fileNameClean(String s)
Returns a "clean" version of the given filename in which spaces have
been converted to dashes and all non-alphanumeric chars are underscores.
|
static boolean |
find(String str,
String regex)
Say whether this regular expression can be found inside
this String.
|
static String |
getBaseName(String fileName)
Strip directory from filename.
|
static String |
getBaseName(String fileName,
String suffix)
Strip directory and suffix from filename.
|
static String |
getBaseName(String fileName,
String suffix,
String sep)
Strip directory and suffix from the given name.
|
static Collection<String> |
getCharacterNgrams(String s,
int minSize,
int maxSize)
Build a list of character-based ngrams from the given string.
|
static Collection<String> |
getNgrams(List<String> words,
int minSize,
int maxSize)
n grams for already split string.
|
static Collection<String> |
getNgramsFromTokens(List<CoreLabel> words,
int minSize,
int maxSize)
n grams for already split string.
|
static Collection<String> |
getNgramsString(String s,
int minSize,
int maxSize)
The string is split on whitespace and the ngrams are joined with a single space
|
static String |
getNotNullString(String s) |
static String |
getShortClassName(Object o)
Returns a short class name for an object.
|
static int |
indexOfRegex(Pattern pattern,
String s) |
static int |
indexOfRegex(Pattern pattern,
String s,
int index) |
static boolean |
isAcronym(String s)
Given a String the method uses Regex to check if the String looks like an acronym
|
static boolean |
isAllUpperCase(String s)
Check if every non whitespace character is upper case (e.g.
|
static boolean |
isAlpha(String s)
Given a String the method uses Regex to check if the String only contains alphabet characters
|
static boolean |
isAlphanumeric(String s)
Given a String the method uses Regex to check if the String only contains alphanumeric characters
|
static boolean |
isCapitalized(String s)
Check if a string begins with an uppercase.
|
static boolean |
isNullOrEmpty(String str)
Returns whether a String is either null or empty.
|
static boolean |
isNumeric(String s)
Given a String the method uses Regex to check if the String only contains numeric characters
|
static boolean |
isPunct(String s)
Given a String the method uses Regex to check if the String only contains punctuation characters
|
static boolean |
isTitleCase(String s)
Check if every word in a string begins with an uppercase and the rest is lower case (e.g.
|
static String |
join(Iterable<?> l)
Joins elements with a space.
|
static <X> String |
join(Iterable<X> l,
String glue)
Joins each elem in the
Iterable with the given glue. |
static <E> String |
join(List<? extends E> l,
String glue,
Function<E,String> toStringFunc,
int start,
int end) |
static String |
join(Object[] elements)
Joins elements with a space.
|
static String |
join(Object[] elements,
int start,
int end,
String glue)
Joins an array of elements in a given span.
|
static String |
join(Object[] elements,
String glue)
Joins each elem in the array with the given glue.
|
static <X> String |
join(Stream<X> l,
String glue)
Joins each elem in the
Stream with the given glue. |
static String |
join(String[] items,
String glue)
Joins each element in the given array with the given glue.
|
static String |
joinFields(List<? extends CoreMap> l,
Class field) |
static String |
joinFields(List<? extends CoreMap> l,
Class field,
Function<Object,String> toStringFunc) |
static String |
joinFields(List<? extends CoreMap> l,
Class field,
String defaultFieldValue,
String glue,
int start,
int end) |
static String |
joinFields(List<? extends CoreMap> l,
Class field,
String defaultFieldValue,
String glue,
int start,
int end,
Function<Object,String> toStringFunc) |
static String |
joinMultipleFields(List<? extends CoreMap> l,
Class[] fields) |
static String |
joinMultipleFields(List<? extends CoreMap> l,
Class[] fields,
Function<Object,String> toStringFunc) |
static String |
joinMultipleFields(List<? extends CoreMap> l,
Class[] fields,
String defaultFieldValue,
String fieldGlue,
String glue,
int start,
int end) |
static String |
joinMultipleFields(List<? extends CoreMap> l,
Class[] fields,
String defaultFieldValue,
String fieldGlue,
String glue,
int start,
int end,
Function<Object,String> toStringFunc) |
static String |
joinWithOriginalWhiteSpace(List<CoreLabel> tokens)
Joins all the tokens together (more or less) according to their original whitespace.
|
static String |
joinWords(Iterable<? extends HasWord> l,
String glue) |
static String |
joinWords(List<? extends HasWord> l,
String glue,
int start,
int end) |
static <E> int |
levenshteinDistance(E[] s1,
E[] s2)
I shamefully stole this from: http://rosettacode.org/wiki/Levenshtein_distance#Java --Gabor
|
static int |
levenshteinDistance(String s1,
String s2)
I shamefully stole this from: http://rosettacode.org/wiki/Levenshtein_distance#Java --Gabor
|
static void |
logInvocationString(Redwood.RedwoodChannels logger,
String[] args)
Logs the command line arguments to Redwood on the given channels.
|
static int |
longestCommonContiguousSubstring(String s,
String t)
Computes the longest common contiguous substring of s and t.
|
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)
Tests the string edit distance function.
|
static String |
makeHTMLTable(String[][] table,
String[] rowLabels,
String[] colLabels)
Returns an HTML table containing the matrix of Strings passed in.
|
static String |
makeTextTable(Object[][] table,
Object[] rowLabels,
Object[] colLabels,
int padLeft,
int padRight,
boolean tsv)
Returns a text table containing the matrix of objects passed in.
|
static String[] |
mapStringToArray(String map)
Takes a string of the form "x1=y1,x2=y2,..." such
that each y is an integer and each x is a key.
|
static Map<String,String> |
mapStringToMap(String map)
Takes a string of the form "x1=y1,x2=y2,..." and returns Map.
|
static boolean |
matches(String str,
String regex)
Say whether this regular expression matches
this String.
|
static String |
normalize(String s) |
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 |
objectToColumnString(Object object,
String delimiter,
String[] fieldNames)
Converts an object into a tab delimited string with given fields
Requires the object has public access for the specified fields
|
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 at the right end with spaces.
|
static String |
pad(String str,
int totalChars,
char pad)
Return a String of length a minimum of totalChars characters by
padding the input String str at the right end 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 |
padLeft(String str,
int totalChars,
char ch)
Pads the given String to the left with the given character ch to ensure that
it's at least totalChars long.
|
static String |
padLeftOrTrim(String str,
int num)
Pad or trim so as to produce a string of exactly a certain length.
|
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<String,String> |
parseCommandLineArguments(String[] args)
A simpler form of command line argument parsing.
|
static Map<String,Object> |
parseCommandLineArguments(String[] args,
boolean parseNumbers)
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(File file,
String message,
boolean append,
boolean printLn,
String encoding)
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 void |
printToFileLn(File file,
String message,
boolean append)
Prints to a file.
|
static void |
printToFileLn(String filename,
String message,
boolean append)
Prints to a file.
|
static LinkedHashMap<String,String> |
propFileToLinkedHashMap(String filename,
Map<String,String> existingArgs)
This method reads in properties listed in a file in the format prop=value,
one property per line.
|
static Properties |
propFileToProperties(String filename)
This method reads in properties listed in a file in the format prop=value, one property per line.
|
static List<Pattern> |
regexesToPatterns(Iterable<String> regexes) |
static List<String> |
regexGroups(Pattern regex,
String str)
Given a pattern, which contains one or more capturing groups, and a String,
returns a list with the values of the
captured groups in the pattern.
|
static String |
repeat(char ch,
int times) |
static String |
repeat(String s,
int times) |
static String |
resolveVars(String str,
Map props)
Resolve variable.
|
static String |
searchAndReplace(String text,
String from,
String to) |
static List<String> |
split(String s)
Splits on whitespace (\\s+).
|
static List<String> |
split(String str,
String regex)
Splits the given string using the given regex as delimiters.
|
static List<List<String>> |
splitFieldsFast(String str,
String delimiter)
Splits a string into whitespace tokenized fields based on a delimiter and then whitespace.
|
static List<String> |
splitKeepDelimiter(String s,
String separator)
Splits on the given delimiter and returns the delimiters as well.
|
static List<String> |
splitLinesKeepNewlines(String s)
Split the text into pieces based on newlines.
|
static void |
splitOnChar(String[] out,
String input,
char delimiter)
Split on a given character, filling out the fields in the output array.
|
static String[] |
splitOnChar(String input,
char delimiter)
Split a string on a given single character.
|
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 Properties |
stringToProperties(String str,
Properties props)
This method updates a Properties object based on
a comma-separated String (with whitespace
optionally allowed after the comma) representing properties
to a Properties object.
|
static Set<String> |
stringToSet(String str,
String delimiter) |
static String |
stripNonAlphaNumerics(String orig) |
static String |
stripSGML(String orig) |
static String |
toAscii(String s) |
static String |
toCSVString(String[] fields) |
static String |
toString(CoreMap sentence)
Convert a CoreMap representing a sentence into a string, by simply joining them with spaces.
|
static String |
toString(List<CoreLabel> words)
Convert a list of labels into a string, by simply joining them with spaces.
|
static String |
toTitleCase(String s) |
static String |
tr(String input,
String from,
String to)
Swap any occurrences of any characters in the from String in the input String with
the corresponding character from the to String.
|
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 |
trimWithEllipsis(Object o,
int width) |
static String |
trimWithEllipsis(String s,
int width) |
static String |
truncate(int n,
int smallestDigit,
int biggestDigit)
This returns a string from decimal digit smallestDigit to decimal digit
biggest digit.
|
static String |
unescapeHtml3(String input)
Unescape an HTML string.
|
static List<String> |
valueSplit(String str,
String valueRegex,
String separatorRegex)
Split a string into tokens.
|
public static final String[] EMPTY_STRING_ARRAY
public static boolean find(String str, String regex)
str
- String to search for match inregex
- String to compile as the regular expressionpublic static boolean containsIgnoreCase(Collection<String> c, String s)
c
- Collection<String>s
- Stringpublic static boolean lookingAt(String str, String regex)
str
- String to search for match at start ofregex
- String to compile as the regular expressionpublic static String[] mapStringToArray(String map)
map
- A string of the form "x1=y1,x2=y2,..." such
that each y is an integer and each x is a key.public static Map<String,String> mapStringToMap(String map)
map
- A string of the form "x1=y1,x2=y2,..."public static List<String> regexGroups(Pattern regex, String str)
public static boolean matches(String str, String regex)
str
- String to search for match at start ofregex
- String to compile as the regular expressionpublic static <E> String join(List<? extends E> l, String glue, Function<E,String> toStringFunc, int start, int end)
public static String joinWords(List<? extends HasWord> l, String glue, int start, int end)
public static String joinFields(List<? extends CoreMap> l, Class field, String defaultFieldValue, String glue, int start, int end, Function<Object,String> toStringFunc)
public static String joinFields(List<? extends CoreMap> l, Class field, String defaultFieldValue, String glue, int start, int end)
public static String joinFields(List<? extends CoreMap> l, Class field, Function<Object,String> toStringFunc)
public static String joinMultipleFields(List<? extends CoreMap> l, Class[] fields, String defaultFieldValue, String fieldGlue, String glue, int start, int end, Function<Object,String> toStringFunc)
public static String joinMultipleFields(List<? extends CoreMap> l, Class[] fields, Function<Object,String> toStringFunc)
public static String joinMultipleFields(List<? extends CoreMap> l, Class[] fields, String defaultFieldValue, String fieldGlue, String glue, int start, int end)
public static String joinMultipleFields(List<? extends CoreMap> l, Class[] fields)
public static String joinWithOriginalWhiteSpace(List<CoreLabel> tokens)
public static <X> String join(Iterable<X> l, String glue)
Iterable
with the given glue.
For example, given a list of Integers
, you can create
a comma-separated list by calling join(numbers, ", ")
.join(Stream, String)
public static <X> String join(Stream<X> l, String glue)
Stream
with the given glue.
For example, given a list of Integers
, you can create
a comma-separated list by calling join(numbers, ", ")
.join(Iterable, String)
public static String join(Object[] elements, String glue)
join(numbers, ", ")
.public static String join(Object[] elements, int start, int end, String glue)
elements
- The elements to join.start
- The start index to join from.end
- The end (non-inclusive) to join until.glue
- The glue to hold together the elements.public static String join(String[] items, String glue)
join(numbers, ", ")
.public static List<String> split(String s)
s
- String to splitpublic static List<String> splitKeepDelimiter(String s, String separator)
s
- String to splitseparator
- Delimiter to use for splittingpublic static List<String> splitLinesKeepNewlines(String s)
s
- String to splitspublic static List<String> split(String str, String regex)
str
- String to split upregex
- String to compile as the regular expressionpublic static String[] splitOnChar(String input, char delimiter)
input
- The input to split.delimiter
- The character to split on.public static List<List<String>> splitFieldsFast(String str, String delimiter)
delimiter
- String to split onpublic static void splitOnChar(String[] out, String input, char delimiter)
out
- The output array to fillinput
- The input to splitdelimiter
- The delimiter to split on.public static List<String> valueSplit(String str, String valueRegex, String separatorRegex)
str
- The String to splitvalueRegex
- Must match a token. You may wish to let it match the empty StringseparatorRegex
- Must match a separatorIllegalArgumentException
- if str cannot be tokenized by the two regexpublic static String pad(String str, int totalChars)
public static String pad(String str, int totalChars, char pad)
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 padLeftOrTrim(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, char ch)
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 repeat(char ch, int times)
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)
-flag1 arg1a arg1b ... arg1m -flag2 -flag3 arg3a ... arg3n
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.public static Map<String,String[]> argsToMap(String[] args, Map<String,Integer> flagsToNumArgs)
-flag1 arg1a arg1b ... arg1m -flag2 -flag3 arg3a ... arg3n
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.)
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)
argsToProperties(String[], Map)
for full documentation.args
- Command line argumentspublic static Properties argsToProperties(String[] args, Map<String,Integer> flagsToNumArgs)
argsToMap(java.lang.String[])
. However, there are several key differences between this method and argsToMap(java.lang.String[])
:
args
- Command line argumentsflagsToNumArgs
- Map of how many arguments flags should have. The keys are without the minus signs.public static Properties propFileToProperties(String filename)
Properties.load(InputStream)
exists, I implemented this method to trim the lines,
something not implemented in the load()
method.filename
- A properties file to readpublic static Properties stringToProperties(String str)
public static Properties stringToProperties(String str, Properties props)
public static String checkRequiredProperties(Properties props, String... requiredProps)
public static void printToFile(File file, String message, boolean append, boolean printLn, String encoding)
append=true
, and overwrites if append=false
.public static void printToFileLn(File file, String message, boolean append)
append=true
, and overwrites if append=false
.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 printToFileLn(String filename, String message, boolean append)
append=true
, and overwrites if append=false
.public static void printToFile(String filename, String message)
public static Map<String,String> parseCommandLineArguments(String[] args)
public static Map<String,Object> parseCommandLineArguments(String[] args, boolean parseNumbers)
public static void printStringOneCharPerLine(String s)
public static String[] splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)
s
- The String to split into fields. Cannot be null.splitChar
- The character to split onquoteChar
- The character to quote items withescapeChar
- The character to escape the quoteChar withpublic static int longestCommonSubstring(String s, String t)
public static int longestCommonContiguousSubstring(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 getShortClassName(Object o)
ArrayList
public static <T> T columnStringToObject(Class objClass, String str, String delimiterRegex, String[] fieldNames) throws InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException
T
- type to returnobjClass
- Class of object to be createdstr
- string to convertdelimiterRegex
- delimiter regular expressionfieldNames
- fieldnamesInstantiationException
IllegalAccessException
NoSuchFieldException
NoSuchMethodException
InvocationTargetException
public static <T> T columnStringToObject(Class<?> objClass, String str, Pattern delimiterPattern, String[] fieldNames) throws InstantiationException, IllegalAccessException, NoSuchMethodException, NoSuchFieldException, InvocationTargetException
T
- type to returnobjClass
- Class of object to be createdstr
- string to convertdelimiterPattern
- delimiterfieldNames
- fieldnamesInstantiationException
IllegalAccessException
NoSuchMethodException
NoSuchFieldException
InvocationTargetException
public static String objectToColumnString(Object object, String delimiter, String[] fieldNames) throws IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException
object
- Object to convertdelimiter
- delimiterfieldNames
- fieldnamesIllegalAccessException
NoSuchFieldException
NoSuchMethodException
InvocationTargetException
public static String capitalize(String s)
s
- a string to capitalizepublic static boolean isCapitalized(String s)
s
- a stringpublic static boolean isTitleCase(String s)
s
- a stringpublic static boolean isAllUpperCase(String s)
s
- a stringpublic static String makeHTMLTable(String[][] table, String[] rowLabels, String[] colLabels)
public static String makeTextTable(Object[][] table, Object[] rowLabels, Object[] colLabels, int padLeft, int padRight, boolean tsv)
public static void main(String[] args)
public static String tr(String input, String from, String to)
Note: This is now optimized to not allocate any objects if the input is returned unchanged.
public static String chomp(String s)
public static String chomp(Object o)
public static String getBaseName(String fileName)
getBaseName("/u/wcmac/foo.txt") ==> "foo.txt"
public static String getBaseName(String fileName, String suffix)
getBaseName("/u/wcmac/foo.txt", "") ==> "foo.txt"
getBaseName("/u/wcmac/foo.txt", ".txt") ==> "foo"
getBaseName("/u/wcmac/foo.txt", ".pdf") ==> "foo.txt"
public static String getBaseName(String fileName, String suffix, String sep)
getBaseName("/tmp/foo/bar/foo", "", "/") ==> "foo"
getBaseName("edu.stanford.nlp", "", "\\.") ==> "nlp"
public static boolean isAlpha(String s)
s
- a String to check using regexpublic static boolean isNumeric(String s)
s
- a String to check using regexpublic static boolean isAlphanumeric(String s)
s
- a String to check using regexpublic static boolean isPunct(String s)
s
- a String to check using regexpublic static boolean isAcronym(String s)
s
- a String to check using regexpublic static boolean isNullOrEmpty(String str)
str
- The String to testpublic static String resolveVars(String str, Map props)
public static Properties argsToPropertiesWithResolve(String[] args)
public static LinkedHashMap<String,String> propFileToLinkedHashMap(String filename, Map<String,String> existingArgs)
filename
- A properties file to readpublic static Collection<String> getNgrams(List<String> words, int minSize, int maxSize)
public static Collection<String> getNgramsFromTokens(List<CoreLabel> words, int minSize, int maxSize)
public static Collection<String> getNgramsString(String s, int minSize, int maxSize)
public static Collection<String> getCharacterNgrams(String s, int minSize, int maxSize)
public static String toString(List<CoreLabel> words)
words
- The words to join.public static String toString(CoreMap sentence)
sentence
- The sentence to stringify.public static int levenshteinDistance(String s1, String s2)
public static <E> int levenshteinDistance(E[] s1, E[] s2)
public static String unescapeHtml3(String input)
input
- The string to unescapepublic static String[] decodeArray(String encoded)
encoded
- The String encoding an arraypublic static Map<String,String> decodeMap(String encoded)
encoded
- The String encoded mappublic static String expandEnvironmentVariables(String raw, Map<String,String> env)
raw
- The raw String to replace variables in.env
- The environment specification; e.g., System.getenv()
.public static String expandEnvironmentVariables(String raw)
System.getenv()
.raw
- The raw String to replace variables in.public static void logInvocationString(Redwood.RedwoodChannels logger, String[] args)
logger
- The redwood logger to log to.args
- The command-line arguments to log.public static int indexOfRegex(Pattern pattern, String s)