public final class StringMethods extends StringUtils
commons-lang3
's StringUtils
class as one-liner methods.CR, EMPTY, LF, SPACE
Constructor and Description |
---|
StringMethods() |
Modifier and Type | Method and Description |
---|---|
static String |
appendPrefix(String prefix,
Character separator,
String body) |
static String |
appendPrefix(String prefix,
String separator,
String body) |
static boolean |
containsAny(CharSequence cs,
Collection<? extends CharSequence> searchChars) |
static boolean |
containsAny(CharSequence cs,
Collection<? extends CharSequence> searchChars,
boolean ignoreCase) |
static int |
countFinds(Collection<String> strings,
String regularExpression)
Counts the total number of occurrences of the given regular expression in the entries.
|
static int |
countMatches(Collection<String> strings,
String regularExpression)
Counts the number of entries in
strings matching the regular expression regularExpression . |
static String |
decrypt(String encryptedKey)
Decrypts the password.
|
static String |
ellipsize(String str,
int maxLength) |
static String |
encloseString(String str,
String startEnclosure,
String endEnclosure) |
static String |
encrypt(String plainKey)
Encrypt the plain text password.
|
static String |
getEnclosedString(String str,
String startEnclosure,
String endEnclosure,
boolean retainEnclosedChar) |
static boolean |
hasAMatch(String string,
Collection<String> regularExpressions)
Check if the given string matches any of the given regular expressions.
|
static String |
humanReadableByteCount(long bytes) |
static String |
humanReadableByteCount(long bytes,
boolean si) |
static String |
implode(Object val,
String separator) |
static boolean |
isEnclosed(String str,
String startEnclosure,
String endEnclosure) |
static String |
join(Object[] array,
String separator,
boolean trimElement) |
static String |
join(String[] strArr,
String charSeperator,
com.google.common.base.Function<String,String> entryTransformer) |
static String |
martiniPackageJoin(Collection<MartiniPackage> col,
String separator) |
static String |
removeEnclosingString(String str,
String startEnclosure,
String endEnclosure) |
static String |
splitCamelCase(String s)
Uses
splitByCharacterTypeCamelCase to split a given string into tokens and
concatenates these tokens via a space. |
static boolean |
startsWithAnyIgnoreCase(CharSequence string,
CharSequence... searchStrings)
Checks if
string starts with any of the items in
searchStrings , regardless of letter case. |
static String |
substitute(Object tokenisedObject)
Substitutes variables within a string by values.
|
static boolean |
substituteAsBoolean(Object tokenisedObject)
Same as substitute, but compares (ignoring case) the result to "true"
and returns that result as a boolean
|
static int |
substituteAsInt(Object tokenisedObject)
Same as substitute, but attempts to parse the result as an int
|
static int |
substituteAsInt(Object tokenisedObject,
int defaultValue)
Same as substitute, but attempts to parse the result as an int.
|
static String[] |
toArray(Object object) |
static String[] |
tokenizeToStringArray(String str,
String delimiter,
boolean trimTokens,
boolean ignoreEmptyTokens,
boolean retainDelimeter)
Splits the provided string into an array, using the given
delimiter as an indicator of where to split. |
static String[] |
tokenizeToStringArray(String str,
String delimiter,
boolean trimTokens,
boolean ignoreEmptyTokens,
boolean retainDelimeter,
int max)
Splits the provided string into an array with a maximum length of
max , using the given delimiter as an indicator of where
to split. |
static String[] |
transformEntry(String[] strArr,
com.google.common.base.Function<String,String> entryTransformer) |
static String |
trimLeadingChar(String str,
String character) |
static String |
trimTrailingChar(String str,
String character) |
abbreviate, abbreviate, abbreviate, abbreviate, abbreviateMiddle, appendIfMissing, appendIfMissingIgnoreCase, capitalize, center, center, center, chomp, chomp, chop, compare, compare, compareIgnoreCase, compareIgnoreCase, contains, contains, containsAny, containsAny, containsAny, containsIgnoreCase, containsNone, containsNone, containsOnly, containsOnly, containsWhitespace, countMatches, countMatches, defaultIfBlank, defaultIfEmpty, defaultString, defaultString, deleteWhitespace, difference, endsWith, endsWithAny, endsWithIgnoreCase, equals, equalsAny, equalsAnyIgnoreCase, equalsIgnoreCase, getCommonPrefix, getDigits, getFuzzyDistance, getJaroWinklerDistance, getLevenshteinDistance, getLevenshteinDistance, indexOf, indexOf, indexOf, indexOf, indexOfAny, indexOfAny, indexOfAny, indexOfAnyBut, indexOfAnyBut, indexOfDifference, indexOfDifference, indexOfIgnoreCase, indexOfIgnoreCase, isAllBlank, isAllEmpty, isAllLowerCase, isAllUpperCase, isAlpha, isAlphanumeric, isAlphanumericSpace, isAlphaSpace, isAnyBlank, isAnyEmpty, isAsciiPrintable, isBlank, isEmpty, isMixedCase, isNoneBlank, isNoneEmpty, isNotBlank, isNotEmpty, isNumeric, isNumericSpace, isWhitespace, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, joinWith, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOfAny, lastIndexOfIgnoreCase, lastIndexOfIgnoreCase, lastOrdinalIndexOf, left, leftPad, leftPad, leftPad, length, lowerCase, lowerCase, mid, normalizeSpace, ordinalIndexOf, overlay, prependIfMissing, prependIfMissingIgnoreCase, remove, remove, removeAll, removeEnd, removeEndIgnoreCase, removeFirst, removeIgnoreCase, removePattern, removeStart, removeStartIgnoreCase, repeat, repeat, repeat, replace, replace, replaceAll, replaceChars, replaceChars, replaceEach, replaceEachRepeatedly, replaceFirst, replaceIgnoreCase, replaceIgnoreCase, replaceOnce, replaceOnceIgnoreCase, replacePattern, reverse, reverseDelimited, right, rightPad, rightPad, rightPad, rotate, split, split, split, split, splitByCharacterType, splitByCharacterTypeCamelCase, splitByWholeSeparator, splitByWholeSeparator, splitByWholeSeparatorPreserveAllTokens, splitByWholeSeparatorPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, startsWith, startsWithAny, startsWithIgnoreCase, strip, strip, stripAccents, stripAll, stripAll, stripEnd, stripStart, stripToEmpty, stripToNull, substring, substring, substringAfter, substringAfterLast, substringBefore, substringBeforeLast, substringBetween, substringBetween, substringsBetween, swapCase, toCodePoints, toEncodedString, toString, trim, trimToEmpty, trimToNull, truncate, truncate, uncapitalize, unwrap, unwrap, upperCase, upperCase, wrap, wrap, wrapIfMissing, wrapIfMissing
public static String humanReadableByteCount(long bytes)
public static String humanReadableByteCount(long bytes, boolean si)
public static String splitCamelCase(String s)
Uses
splitByCharacterTypeCamelCase
to split a given string into tokens and
concatenates these tokens via a space.
StringUtils.splitCamelCase(null) = null StringUtils.splitCamelCase("") = "" StringUtils.splitCamelCase("ab de fg") = "ab de fg" StringUtils.splitCamelCase("ab de fg") = "ab de fg" StringUtils.splitCamelCase("ab:cd:ef") = "ab: cd: ef" StringUtils.splitCamelCase("number5") = "number 5" StringUtils.splitCamelCase("fooBar") = "foo Bar" StringUtils.splitCamelCase("foo200Bar") = "foo 200 Bar" StringUtils.splitCamelCase("ASFRules") = "ASF Rules"
s
- the string to splitnull
, if the string to split is null
; otherwise, the split stringpublic static String encrypt(String plainKey) throws ToroException
plainKey
- The password.Exception
- If an error occurs.ToroException
public static String decrypt(String encryptedKey)
encryptedKey
- the encrypted password.Exception
- If an error occurs.public static String[] tokenizeToStringArray(String str, String delimiter, boolean trimTokens, boolean ignoreEmptyTokens, boolean retainDelimeter)
delimiter
as an indicator of where to split.
StringUtils.tokenizeToStringArray(null, ":", false, false, false) = null StringUtils.tokenizeToStringArray("ab:cd:ef", null, false, false, false) = null StringUtils.tokenizeToStringArray(" ab : cd : ef ", ":", true, false, false) = ["ab", "cd", "ef"] StringUtils.tokenizeToStringArray("ab: :cd::ef", ":", false, true, false) = ["ab", " ", "cd", "ef"] StringUtils.tokenizeToStringArray("ab: :cd::ef", ":", false, false, false) = ["ab", " ", "cd", "", "ef"] StringUtils.tokenizeToStringArray("ab:cd:ef", ":", false, false, true) = ["ab", ":", "cd", ":", "ef"]
str
- the string to splitdelimiter
- indicates where to split str
trimTokens
- when set to true, trailing whitespaces are removed from every elementignoreEmptyTokens
- when set to true, empty elements ("") are removed from the arrayretainDelimeter
- when set to true, delimiter
s in the array will be retained (such that they belong
in their own array index)null
if str
or delimiter
is null
public static String[] tokenizeToStringArray(String str, String delimiter, boolean trimTokens, boolean ignoreEmptyTokens, boolean retainDelimeter, int max)
max
, using the given delimiter
as an indicator of where
to split.
StringUtils.tokenizeToStringArray(null, ":", false, false, false, 0) = null StringUtils.tokenizeToStringArray("ab:cd:ef", null, false, false, false, 0) = null StringUtils.tokenizeToStringArray(" ab : cd : ef ", ":", true, false, false, 0) = ["ab", "cd", "ef"] StringUtils.tokenizeToStringArray("ab: :cd::ef", ":", false, true, false, 0) = ["ab", " ", "cd", "ef"] StringUtils.tokenizeToStringArray("ab: :cd::ef", ":", false, false, false, 0) = ["ab", " ", "cd", "", "ef"] StringUtils.tokenizeToStringArray("ab:cd:ef", ":", false, false, true, 0) = ["ab", ":", "cd", ":", "ef"] StringUtils.tokenizeToStringArray("ab:cd:ef", ":", false, false, false, 2) = ["ab", "cd:ef"]
str
- the string to splitdelimiter
- indicates where to split str
trimTokens
- when set to true, trailing whitespaces are removed from every elementignoreEmptyTokens
- when set to true, empty elements ("") are removed from the arrayretainDelimeter
- when set to true, delimiter
s in the array will be retained (such that they belong
in their own array index)max
- the maximum number of elements to include in the array; a zero or negative value implies no limitnull
if str
or delimiter
is null
public static String removeEnclosingString(String str, String startEnclosure, String endEnclosure)
public static String encloseString(String str, String startEnclosure, String endEnclosure)
public static String getEnclosedString(String str, String startEnclosure, String endEnclosure, boolean retainEnclosedChar)
public static String martiniPackageJoin(Collection<MartiniPackage> col, String separator)
public static boolean containsAny(CharSequence cs, Collection<? extends CharSequence> searchChars)
public static boolean containsAny(CharSequence cs, Collection<? extends CharSequence> searchChars, boolean ignoreCase)
public static String join(String[] strArr, String charSeperator, com.google.common.base.Function<String,String> entryTransformer)
public static boolean startsWithAnyIgnoreCase(CharSequence string, CharSequence... searchStrings)
Checks if string
starts with any of the items in
searchStrings
, regardless of letter case.
StringUtils.startsWithAnyIgnoreCase("ABCD", ["AB", "xy"]) = true StringUtils.startsWithAnyIgnoreCase("ABCD", ["ab", "xy"]) = true StringUtils.startsWithAnyIgnoreCase("abcd", ["gh", "xy"]) = false StringUtils.startsWithAnyIgnoreCase("abcd", null) = false StringUtils.startsWithAnyIgnoreCase("", ["abc", ""]) = true StringUtils.startsWithAnyIgnoreCase(null, ["abc", null]) = true StringUtils.startsWithAnyIgnoreCase(null, ["abc", "null"]) = false
string
- the string to checksearchStrings
- the prefixes to look fortrue
if string
starts with any of the items in searchStrings
(case insensitive);
false
if searchStrings
is null
or empty.public static String[] transformEntry(String[] strArr, com.google.common.base.Function<String,String> entryTransformer)
public static int countMatches(Collection<String> strings, String regularExpression)
Counts the number of entries in strings
matching the regular expression regularExpression
.
A null
collection of entries or regular expression matches none (thus, zero is returned).
strings
- the entries to checkregularExpression
- the regular expression to match againststrings
matching regularExpression
; zero if either strings
or regularExpression
is null
countFinds(Collection, String)
public static int countFinds(Collection<String> strings, String regularExpression)
Counts the total number of occurrences of the given regular expression in the entries.
A null
collection of entries or regular expression matches none (thus, zero is returned).
strings
- the entries to checkregularExpression
- the regular expression to findstrings
matching regularExpression
; zero if either strings
or regularExpression
is null
countMatches(Collection, String)
public static boolean hasAMatch(String string, Collection<String> regularExpressions)
Check if the given string matches any of the given regular expressions. Results to no matches if either
string
or regularExpressions
is null
.
public static String substitute(Object tokenisedObject)
String templateString = StringMethods.substitute( "The ${animal} jumped over the ${target}. ${undefined .number:-1234567890}." );Resulting in:
The quick brown fox jumped over the lazy dog. 1234567890.
tokenisedObject
- public static int substituteAsInt(Object tokenisedObject) throws NumberFormatException
tokenisedObject
- NumberFormatException
- when the result of the expression can't be parsed as an intpublic static int substituteAsInt(Object tokenisedObject, int defaultValue)
tokenisedObject
- public static boolean substituteAsBoolean(Object tokenisedObject)
tokenisedObject
- Copyright © 2019. All rights reserved.