public class StringUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
EMPTY_STRING |
static String[] |
EMPTY_STRING_ARRAY |
static int |
SHORTEN_END |
static int |
SHORTEN_MIDDLE |
static int |
SHORTEN_START |
Modifier and Type | Method and Description |
---|---|
static String |
bytesToHex(byte[] bytes)
Convert an array of arbitrary bytes into a String of hexadecimal number-pairs with each pair representing on byte
of the array.
|
static int |
countOccurrences(String haystack,
char needle)
Counts the number of occurrences of the given character.
|
static int |
countOccurrences(String haystack,
String needle)
Counts the number of occurrences of the given character.
|
static byte[] |
hexToBytes(String hex)
Convert a string consisting of hex-numbers into an array of bytes, which contains the binary representation of
those hexadecimal-numbers (takes pairs of numbers, so make sure the number of characters is even).
|
static boolean |
isEmpty(String text)
Universal check on String-objects that even works on non-instantiated variables (null-references), as it checks for
null first.
|
static String |
md5Hash(String text)
Compute the MD-5 hash of a string, and return it has a string of hexadecimal numbers.
|
static int |
md5HashInt(String text)
Compute the MD-5 hash of a string, and return a truncated int-value of the hash.
|
static long |
md5HashLong(String text)
Compute the MD-5 hash of a string, and return a truncated long-value of the hash.
|
static String |
sha1Hash(String text)
Compute the SHA-1 hash of a string, and return it has a string of hexadecimal numbers.
|
static int |
sha1HashInt(String text)
Compute the SHA-1 hash of a string, and return a truncated int-value of the hash.
|
static long |
sha1HashLong(String text)
Compute the SHA-1 hash of a string, and return a truncated long-value of the hash.
|
static String |
shorten(String text,
int size,
int mode)
Shortens a given String "text" down to size length, indicating the shortening by three dots ("...").
|
static String[] |
simpleSplit(String string,
char delimiter)
|
static String[] |
simpleSplit(String string,
char delimiter,
int startOffset)
|
static int |
truncateHashToInt(byte[] bytes)
Computes a truncated code from the given hash-value and returns it as int-variable.
|
static int |
truncateHashToInt(String hash)
Computes a truncated code from the given hash-value and returns it as int-variable.
|
static long |
truncateHashToLong(byte[] bytes)
Computes a truncated code from the given hash-value and returns it as long-variable.
|
static long |
truncateHashToLong(String hash)
Computes a truncated code from the given hash-value and returns it as long-variable.
|
public static final int SHORTEN_START
public static final int SHORTEN_MIDDLE
public static final int SHORTEN_END
public static final String EMPTY_STRING
public static final String[] EMPTY_STRING_ARRAY
public static boolean isEmpty(String text)
text
- The string-object to be checkedpublic static String bytesToHex(byte[] bytes)
bytes
- the array to convert into hexadecimal stringpublic static byte[] hexToBytes(String hex)
hex
- String of pairs of hexadecimal numberspublic static String sha1Hash(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException
text
- Text to compute the SHA-1 hash ofNoSuchAlgorithmException
- thrown if the SHA-1 algorithm cannot be foundUnsupportedEncodingException
- thrown if the encoding is not supportedpublic static int sha1HashInt(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException
text
- Text to compute the SHA-1 hash ofNoSuchAlgorithmException
- thrown if the SHA-1 algorithm cannot be foundUnsupportedEncodingException
- thrown if the encoding is not supportedpublic static long sha1HashLong(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException
text
- Text to compute the SHA-1 hash ofNoSuchAlgorithmException
- thrown if the SHA-1 algorithm cannot be foundUnsupportedEncodingException
- thrown if the encoding is not supportedpublic static String md5Hash(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException
text
- Text to compute the MD-5 hash ofNoSuchAlgorithmException
- thrown if the MD-5 algorithm cannot be foundUnsupportedEncodingException
- thrown if the encoding is not supportedpublic static int md5HashInt(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException
text
- Text to compute the MD-5 hash ofNoSuchAlgorithmException
- thrown if the MD-5 algorithm cannot be foundUnsupportedEncodingException
- thrown if the encoding is not supportedpublic static long md5HashLong(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException
text
- Text to compute the MD-5 hash ofNoSuchAlgorithmException
- thrown if the MD-5 algorithm cannot be foundUnsupportedEncodingException
- thrown if the encoding is not supportedpublic static int truncateHashToInt(String hash)
hash
- the hash-stringpublic static int truncateHashToInt(byte[] bytes)
bytes
- hash-value as a byte-arraypublic static long truncateHashToLong(String hash)
hash
- the hash-stringpublic static long truncateHashToLong(byte[] bytes)
bytes
- the hash-value as a byte-arraypublic static String shorten(String text, int size, int mode)
text
- The string to shortensize
- The size the text is to shortened to.mode
- The mode in which the string shall be shortenedpublic static int countOccurrences(String haystack, char needle)
haystack
- The string in which to search for the given character.needle
- The character to search for.public static int countOccurrences(String haystack, String needle)
haystack
- The string in which to search for the given character.needle
- The character to search for.public static String[] simpleSplit(String string, char delimiter)
Performs a simple splitting of the given String
string
at the given delimiter
. Opposite to
the method String.split(java.lang.String)
, this method does not use regular expressions, but a simple
character matching algorithm. That means, this method is much faster, though less flexible regarding the
delimiters.
This simpleSplit(java.lang.String, char)
method always starts a the beginning of the given
string
.
public static String[] simpleSplit(String string, char delimiter, int startOffset)
Performs a simple splitting of the given String
string
at the given delimiter
. Opposite to
the method String.split(java.lang.String)
, this method does not use regular expressions, but a simple
character matching algorithm. That means, this method is much faster, though less flexible regarding the
delimiters.
This simpleSplit(java.lang.String, char, int)
method starts at the given startOffset
of the given
string
, which means it can be any position within the string.
Copyright © 2015. All Rights Reserved.