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