public final class StringHelper extends Object
Modifier and Type | Method and Description |
---|---|
static String |
after(String text,
String after)
Returns the string after the given token
|
static <T> Optional<T> |
after(String text,
String after,
Function<String,T> mapper)
Returns an object after the given token
|
static String |
before(String text,
String before)
Returns the string before the given token
|
static <T> Optional<T> |
before(String text,
String before,
Function<String,T> mapper)
Returns an object before the given token
|
static String |
between(String text,
String after,
String before)
Returns the string between the given tokens
|
static <T> Optional<T> |
between(String text,
String after,
String before,
Function<String,T> mapper)
Returns an object between the given token
|
static String |
betweenOuterPair(String text,
char before,
char after)
Returns the string between the most outer pair of tokens
The number of token pairs must be evenly, eg there must be same number of before and after tokens, otherwise null is returned
This implementation skips matching when the text is either single or double quoted.
|
static <T> Optional<T> |
betweenOuterPair(String text,
char before,
char after,
Function<String,T> mapper)
Returns an object between the most outer pair of tokens
|
static String |
capitalize(String text)
Capitalize the string (upper case first character)
|
static List<Integer> |
changedLines(String oldText,
String newText)
Compares old and new text content and report back which lines are changed
|
static boolean |
containsIgnoreCase(String src,
String what)
Checks if the src string contains what
|
static int |
countChar(String s,
char ch)
Counts the number of times the given char is in the string
|
static boolean |
hasStartToken(String expression,
String language)
Does the expression have the language start token?
|
static boolean |
hasUpperCase(String text)
Determines if the string has at least one letter in upper case
|
static String |
humanReadableBytes(Locale locale,
long bytes)
Outputs the bytes in human readable format in units of KB,MB,GB etc.
|
static String |
humanReadableBytes(long bytes)
Outputs the bytes in human readable format in units of KB,MB,GB etc.
|
static boolean |
isClassName(String text)
Determines if the string is a fully qualified class name
|
static boolean |
isJavaIdentifier(String name)
Returns true if the given name is a valid java identifier
|
static boolean |
isQuoted(String s)
Whether the string starts and ends with either single or double quotes.
|
static String |
limitLenght(String s,
int maxLength)
Deprecated.
|
static String |
limitLength(String s,
int maxLength)
Limits the length of a string
|
static boolean |
matches(String patter,
String target)
Check for string pattern matching with a number of strategies in the
following order:
- equals
- null pattern always matches
- * always matches
- Ant style matching
- Regexp
|
static String |
normalizeClassName(String name)
Cleans the string to a pure Java identifier so we can use it for loading class names.
|
static String |
notEmpty(String value,
String name)
Asserts whether the string is not empty.
|
static String |
notEmpty(String value,
String name,
Object on)
Asserts whether the string is not empty.
|
static String |
removeCRLF(String s)
Remove carriage return and line feeds from a String, replacing them with an empty String.
|
static String |
removeLeadingAndEndingQuotes(String s)
Removes all leading and ending quotes (single and double) from the string
|
static String |
removeQuotes(String s)
Removes all quotes (single and double) from the string
|
static String |
removeStartingCharacters(String text,
char ch)
Removes any starting characters on the given text which match the given
character
|
static String |
replaceAll(String input,
String from,
String to)
Replaces all the from tokens in the given input string.
|
static String |
sanitize(String s)
Ensures that
s is friendly for a URL or file system. |
static String[] |
splitOnCharacter(String value,
String needle,
int count) |
static String |
toJson(String name,
String value,
boolean isMap)
Creates a json tuple with the given name/value pair.
|
static String |
trimToNull(String given)
Removes the leading and trailing whitespace and if the resulting
string is empty returns
null . |
static String |
xmlEncode(String text)
Encodes the text into safe XML by replacing < > and & with XML tokens
|
public static String sanitize(String s)
s
is friendly for a URL or file system.s
- String to be sanitized.s
.NullPointerException
- if s
is null
.public static String removeCRLF(String s)
s
- String to be sanitized of carriage return / line feed characterss
.NullPointerException
- if s
is null
.public static int countChar(String s, char ch)
s
- the stringch
- the char@Deprecated public static String limitLenght(String s, int maxLength)
limitLength(String, int)
s
- the stringmaxLength
- the maximum length of the returned stringpublic static String limitLength(String s, int maxLength)
s
- the stringmaxLength
- the maximum length of the returned stringpublic static String removeQuotes(String s)
s
- the stringpublic static String removeLeadingAndEndingQuotes(String s)
s
- the stringpublic static boolean isQuoted(String s)
s
- the stringpublic static String xmlEncode(String text)
text
- the textpublic static boolean hasUpperCase(String text)
text
- the textpublic static boolean isClassName(String text)
public static boolean hasStartToken(String expression, String language)
expression
- the expressionlanguage
- the name of the language, such as simplepublic static String replaceAll(String input, String from, String to)
input
- the input stringfrom
- the from string, must not be null or emptyto
- the replacement string, must not be emptyIllegalArgumentException
- if the input arguments is invalidpublic static String toJson(String name, String value, boolean isMap)
name
- the namevalue
- the valueisMap
- whether the tuple should be mappublic static String notEmpty(String value, String name)
value
- the string to testname
- the key that resolved the valuevalue
as isIllegalArgumentException
- is thrown if assertion failspublic static String notEmpty(String value, String name, Object on)
value
- the string to teston
- additional description to indicate where this problem occurred (appended as toString())name
- the key that resolved the valuevalue
as isIllegalArgumentException
- is thrown if assertion failspublic static String[] splitOnCharacter(String value, String needle, int count)
public static String removeStartingCharacters(String text, char ch)
text
- the stringch
- the initial characters to removepublic static String capitalize(String text)
text
- the stringpublic static String after(String text, String after)
text
- the textafter
- the tokenpublic static <T> Optional<T> after(String text, String after, Function<String,T> mapper)
text
- the textafter
- the tokenmapper
- a mapping function to convert the string after the token to type Tpublic static String before(String text, String before)
text
- the textbefore
- the tokenpublic static <T> Optional<T> before(String text, String before, Function<String,T> mapper)
text
- the textbefore
- the tokenmapper
- a mapping function to convert the string before the token to type Tpublic static String between(String text, String after, String before)
text
- the textafter
- the before tokenbefore
- the after tokenpublic static <T> Optional<T> between(String text, String after, String before, Function<String,T> mapper)
text
- the textafter
- the before tokenbefore
- the after tokenmapper
- a mapping function to convert the string between the token to type Tpublic static String betweenOuterPair(String text, char before, char after)
text
- the textafter
- the before tokenbefore
- the after tokenpublic static <T> Optional<T> betweenOuterPair(String text, char before, char after, Function<String,T> mapper)
text
- the textafter
- the before tokenbefore
- the after tokenmapper
- a mapping function to convert the string between the most outer pair of tokens to type Tpublic static boolean isJavaIdentifier(String name)
public static String normalizeClassName(String name)
name
- the class namepublic static List<Integer> changedLines(String oldText, String newText)
oldText
- the old textnewText
- the new textpublic static String trimToNull(String given)
null
. Examples:
Examples:
trimToNull("abc") -> "abc" trimToNull(" abc") -> "abc" trimToNull(" abc ") -> "abc" trimToNull(" ") -> null trimToNull("") -> null
public static boolean containsIgnoreCase(String src, String what)
src
- is the source string to be checkedwhat
- is the string which will be looked up in the src argumentpublic static String humanReadableBytes(Locale locale, long bytes)
locale
- The locale to apply during formatting. If l is null
then no localization is applied.bytes
- number of bytesString.format(Locale, String, Object...)
public static String humanReadableBytes(long bytes)
Locale.getDefault()
.bytes
- number of byteshumanReadableBytes(Locale, long)
public static boolean matches(String patter, String target)
patter
- the patterntarget
- the string to testApache Camel