|
TUM CCSM Commons | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.tum.cs.commons.string.StringUtils
public class StringUtils
A utility class providing some advanced string functionality.
Field Summary | |
---|---|
static java.lang.String |
CR
Line break. |
static java.lang.String |
EMPTY_STRING
The emtpy string. |
static java.lang.String |
LINE_TERMINATOR_SYMBOL
A symbol representing the line terminator. |
static java.lang.String |
SPACE
A space. |
static char |
SPACE_CHAR
A space character. |
static java.lang.String |
TAB
A tab character. |
static java.lang.String |
TWO_SPACES
Two spaces. |
Constructor Summary | |
---|---|
StringUtils()
|
Method Summary | |
---|---|
static java.lang.String |
addRandomLineBreaks(java.lang.String text,
int count)
Adds random line breaks to a string. |
static java.lang.String |
bytesToString(byte[] b)
Converts a (UTF-8) byte array to a string. |
static java.lang.String |
camelCaseToHyphenated(java.lang.String s)
Transforms a string from camel-case to lower-case with hyphens. |
static java.lang.String |
capitalize(java.lang.String string)
Capitalize string. |
static java.lang.String |
center(java.lang.String string,
int length,
char c)
Create a sting of the given length and center the given string within it. |
static java.lang.String |
concat(java.lang.Iterable<?> iterable)
Concatenates all elements of an iterable using the toString() -method. |
static java.lang.String |
concat(java.lang.Iterable<?> iterable,
java.lang.String separator)
Concatenates all elements of an iterable using the toString() -method, separating them with the given
separator . |
static java.lang.String |
concat(java.lang.Object[] array)
Concatenates all elements of an array using the toString()
-method. |
static java.lang.String |
concat(java.lang.Object[] array,
java.lang.String separator)
Concatenates all elements of an array using the toString()
-method, separating them with the given separator . |
static java.lang.String[] |
concat(java.lang.String[] array1,
java.lang.String[] array2)
Concatenate two string arrays. |
static boolean |
containsOneOf(java.lang.String text,
java.lang.String... strings)
Test if a string contains of the provided strings. |
static java.lang.String |
createUniqueName(java.lang.String baseName,
java.util.Set<java.lang.String> usedNames)
Creates a unique name which is not contained in the given set of names. |
static byte[] |
decodeFromHex(java.lang.String s)
Decodes a byte array from a hex string. |
static java.lang.String |
encodeAsHex(byte[] data)
Encodes a byte array as a hex string. |
static boolean |
endsWithOneOf(java.lang.String string,
java.lang.String... suffixes)
Test if a string ends with one of the provided suffixes. |
static java.lang.String |
escapeRegexReplacementString(java.lang.String replacement)
Regex replacement methods like Matcher.appendReplacement(StringBuffer, String) or
String.replaceAll(String, String) treat dollar signs as group
references. |
static java.lang.String |
fillString(int length,
char c)
Build a string with a specified length from a character. |
static java.lang.String |
flushLeft(java.lang.String string,
int length,
char c)
Create a sting of the given length starting with the provided string. |
static java.lang.String |
flushRight(java.lang.String string,
int length,
char c)
Create a sting of the given length ending with the provided string. |
static java.lang.String |
format(double number,
java.text.NumberFormat numberFormat)
Format number with number formatter, if number formatter is null , this uses String.valueOf(double) . |
static java.lang.String |
format(java.lang.Number number)
Format number |
static java.lang.String |
formatAsPercentage(java.lang.Number number)
Format as percentage. |
static java.lang.String |
generateString(int length,
int seed)
Generates a pseudo random string with a certain length in a deterministic, reproducable fashion. |
static java.lang.String[] |
generateStringArray(int length,
int stringLength,
int seed)
Generates an array of pseudo-random strings in a deterministic, reproducable fashion. |
static java.lang.String |
getFirstLine(java.lang.String string)
Returns the first line of a string. |
static java.lang.String |
getFirstParts(java.lang.String string,
int partNumber,
char separator)
Returns the first n part of a string, separated by the given character. |
static java.util.HashMap<java.lang.String,java.lang.String> |
getKeyValuePairs(java.lang.String keyValueString)
Splits a key-value string and stores it in a hash map. |
static java.lang.String |
getLastPart(java.lang.String string,
char separator)
Return the last part of a String which is separated by the given character. |
static int |
indexOf(java.lang.String[] array,
java.lang.String string)
Searches the elements of a string array for a string. |
static boolean |
isEmpty(java.lang.String text)
Checks if a string is empty (after trimming). |
static java.lang.String |
longestCommonPrefix(java.lang.Iterable<java.lang.String> strings)
Returns the longest common prefix of the strings in the list or the empty string if no common prefix exists. |
static java.lang.String |
longestCommonPrefix(java.lang.String s,
java.lang.String t)
Returns the longest common prefix of s and t |
static java.lang.String |
normalizeLineBreaks(java.lang.String string)
Replace all linebreaks in string with the platform-specific line separator. |
static java.lang.String |
obtainStackTrace(java.lang.Throwable throwable)
Convert stack trace of a Throwable to a string. |
static java.lang.String |
prefixLines(java.lang.String string,
java.lang.String prefix,
boolean prefixFirstLine)
Prefix all lines of a string. |
static java.lang.String |
randomString(int length)
Generates a random string with a certain length. |
static java.lang.String[] |
randomStringArray(int length,
int stringLength)
Generates an array of random strings. |
static java.lang.String |
removeLastPart(java.lang.String string,
char separator)
Returns the beginning of a String, cutting off the last part which is separated by the given character. |
static java.lang.String |
removeWhitespace(java.lang.String content)
Removes whitespace from a string. |
static java.lang.String |
replaceLineBreaks(java.lang.String string)
Replace all linebreaks in string with LINE_TERMINATOR_SYMBOL . |
static java.lang.String |
replaceLineBreaks(java.lang.String string,
java.lang.String symbol)
Replace all linebreaks in string by a specified symbol. |
static java.lang.Character[] |
splitChars(java.lang.String s)
Splits the given string into an array of Character s. |
static java.lang.String[] |
splitLines(java.lang.String content)
Split string in lines. |
static java.util.List<java.lang.String> |
splitLinesAsList(java.lang.String content)
Split string in lines. |
static boolean |
startsWithOneOf(java.lang.String string,
java.lang.String... prefixes)
Test if a string starts with one of the provided prefixes. |
static byte[] |
stringToBytes(java.lang.String s)
Converts a string to a (UTF-8) byte representation. |
static java.lang.String |
stripDigits(java.lang.String string)
Strips all digits from the given String. |
static java.lang.String |
stripPrefix(java.lang.String prefix,
java.lang.String string)
Remove prefix from a string. |
static java.lang.String |
stripSuffix(java.lang.String suffix,
java.lang.String string)
Remove suffix from a string. |
static java.lang.String |
toString(java.util.Map<?,?> map)
Create string representation of a map. |
static java.lang.String |
toString(java.util.Map<?,?> map,
java.lang.String indent)
Create string representation of a map. |
static java.lang.String |
wrapLongLines(java.lang.String s,
int maxLineLength)
This method splits the input string into words (delimited by whitespace) and returns a string whose words are separated by single spaces and whose lines are not longer than the given length (unless a very long word occurs)). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String CR
public static final java.lang.String EMPTY_STRING
public static final java.lang.String LINE_TERMINATOR_SYMBOL
public static final java.lang.String SPACE
public static final char SPACE_CHAR
public static final java.lang.String TAB
public static final java.lang.String TWO_SPACES
Constructor Detail |
---|
public StringUtils()
Method Detail |
---|
public static java.lang.String addRandomLineBreaks(java.lang.String text, int count)
text
- the original string.count
- the number of line breaks to add.
public static java.lang.String center(java.lang.String string, int length, char c)
string
- The input string.length
- The length of the string to be returned.c
- The character to surround the input string with.
flushLeft(String, int, char)
,
flushRight(String, int, char)
public static java.lang.String concat(java.lang.Iterable<?> iterable)
toString()
-method.
iterable
- the iterable
public static java.lang.String concat(java.lang.Iterable<?> iterable, java.lang.String separator)
toString()
-method, separating them with the given
separator
.
iterable
- the iterable containing the stringsseparator
- the separator to place between the strings, may be
null
null
if array was null
. If array is of length 0 the empty
string is returned.public static java.lang.String concat(java.lang.Object[] array)
toString()
-method.
array
- the array containing the strings
public static java.lang.String concat(java.lang.Object[] array, java.lang.String separator)
toString()
-method, separating them with the given separator
.
array
- the arrayseparator
- the separator to place between the strings, may be
null
null
if array was null
. If array is of length 0 the empty
string is returned.public static java.lang.String[] concat(java.lang.String[] array1, java.lang.String[] array2)
array1
- array2
-
public static java.lang.String fillString(int length, char c)
length
- The length of the string.c
- The character.
public static java.lang.String flushLeft(java.lang.String string, int length, char c)
string
- The input string.length
- The length of the string to be returned.c
- The character to fill the string.
flushRight(String, int, char)
,
center(String, int, char)
public static java.lang.String flushRight(java.lang.String string, int length, char c)
string
- The input string.length
- The length of the string to be returned.c
- The character to fill the string.
flushLeft(String, int, char)
,
center(String, int, char)
public static java.lang.String format(java.lang.Number number)
public static java.lang.String formatAsPercentage(java.lang.Number number)
public static java.lang.String getFirstLine(java.lang.String string)
public static java.lang.String getFirstParts(java.lang.String string, int partNumber, char separator)
string
- the base stringpartNumber
- number of partsseparator
- the separator characterpublic static java.util.HashMap<java.lang.String,java.lang.String> getKeyValuePairs(java.lang.String keyValueString)
key=value[,key=value]*
null
null
is returned.
keyValueString
- with format described above
public static java.lang.String getLastPart(java.lang.String string, char separator)
string
- the Stringseparator
- separation character
public static int indexOf(java.lang.String[] array, java.lang.String string)
array
- the array to searchstring
- the search string
-1
if string wasn't found.public static boolean isEmpty(java.lang.String text)
text
- the string to check.
true
if string is empty or null
,
false
otherwise.public static java.lang.String randomString(int length)
length
- the length of the random string
public static java.lang.String[] randomStringArray(int length, int stringLength)
length
- number of stringsstringLength
- length of each string
public static java.lang.String generateString(int length, int seed)
length
- the length of the pseudo-random stringseed
- seed value for the random number generator used for the
generation of the pseudo-random string. If the same seed value
is used, the same pseudo-random string is generated.public static java.lang.String[] generateStringArray(int length, int stringLength, int seed)
length
- number of stringsstringLength
- length of each stringseed
- seed value for the random number generator used for the
generation of the pseudo-random string. If the same seed value
is used, the same pseudo-random string array is generated.
public static java.lang.String removeLastPart(java.lang.String string, char separator)
string
- the Stringseparator
- separation character
public static java.lang.String replaceLineBreaks(java.lang.String string)
LINE_TERMINATOR_SYMBOL
.
public static java.lang.String normalizeLineBreaks(java.lang.String string)
public static java.lang.String replaceLineBreaks(java.lang.String string, java.lang.String symbol)
public static java.lang.String[] splitLines(java.lang.String content)
null
an
array of length zero is returned.
splitLinesAsList(String)
public static java.util.List<java.lang.String> splitLinesAsList(java.lang.String content)
null
an
empty list is returned.
splitLines(String)
public static java.lang.String stripPrefix(java.lang.String prefix, java.lang.String string)
prefix
- the prefixstring
- the string
public static java.lang.String stripSuffix(java.lang.String suffix, java.lang.String string)
suffix
- the suffixstring
- the string
public static java.lang.String stripDigits(java.lang.String string)
public static java.lang.String toString(java.util.Map<?,?> map)
public static java.lang.String toString(java.util.Map<?,?> map, java.lang.String indent)
map
- the mapindent
- a line indentpublic static java.lang.String obtainStackTrace(java.lang.Throwable throwable)
Throwable
to a string.
public static boolean startsWithOneOf(java.lang.String string, java.lang.String... prefixes)
false
if the list of prefixes is empty. This should only be
used for short lists of prefixes.
public static boolean containsOneOf(java.lang.String text, java.lang.String... strings)
false
if the list of strings is empty. This should only be
used for short lists of strings.
public static boolean endsWithOneOf(java.lang.String string, java.lang.String... suffixes)
false
if the list of prefixes is empty. This should only be
used for short lists of suffixes.
public static java.lang.String prefixLines(java.lang.String string, java.lang.String prefix, boolean prefixFirstLine)
string
- the string to prefixprefix
- the prefix to addprefixFirstLine
- a flag that indicates if the first line should be prefixed or
not.public static java.lang.Character[] splitChars(java.lang.String s)
Character
s. This is
mostly used for testing purposes, if an array of certain objects is
needed.
public static java.lang.String capitalize(java.lang.String string)
public static java.lang.String wrapLongLines(java.lang.String s, int maxLineLength)
public static java.lang.String longestCommonPrefix(java.lang.String s, java.lang.String t)
public static java.lang.String longestCommonPrefix(java.lang.Iterable<java.lang.String> strings)
public static java.lang.String removeWhitespace(java.lang.String content)
public static java.lang.String createUniqueName(java.lang.String baseName, java.util.Set<java.lang.String> usedNames)
public static java.lang.String camelCaseToHyphenated(java.lang.String s)
public static java.lang.String encodeAsHex(byte[] data)
public static byte[] decodeFromHex(java.lang.String s)
public static java.lang.String format(double number, java.text.NumberFormat numberFormat)
null
, this uses String.valueOf(double)
.
public static java.lang.String escapeRegexReplacementString(java.lang.String replacement)
Matcher.appendReplacement(StringBuffer, String)
or
String.replaceAll(String, String)
treat dollar signs as group
references. This method escapes replacement strings so that dollar signs
are treated as literals.
public static byte[] stringToBytes(java.lang.String s)
public static java.lang.String bytesToString(byte[] b)
|
TUM CCSM Commons | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |