Package org.greenrobot.essentials
Class StringUtils
- java.lang.Object
-
- org.greenrobot.essentials.StringUtils
-
public class StringUtils extends java.lang.Object
Utilities for working with strings like splitting, joining, url-encoding, hex, and digests.
-
-
Field Summary
Fields Modifier and Type Field Description private static char[]
HEX_CHARS
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
decodeUrl(java.lang.String stringToDecode)
URL-Decodes a given string using UTF-8.static java.lang.String
decodeUrlIso(java.lang.String stringToDecode)
URL-Decodes a given string using ISO-8859-1.static java.lang.String
digest(java.lang.String string, java.lang.String digestAlgo, java.lang.String encoding)
Generates a digest (hex string) for the given stringstatic java.lang.String
ellipsize(java.lang.String text, int maxLength)
Cuts the string at the end if it's longer than maxLength and appends "..." to it.static java.lang.String
ellipsize(java.lang.String text, int maxLength, java.lang.String end)
Cuts the string at the end if it's longer than maxLength and appends the given end string to it.static java.lang.String
encodeUrl(java.lang.String stringToEncode)
URL-Encodes a given string using UTF-8 (some web pages have problems with UTF-8 and umlauts, considerencodeUrlIso(String)
also).static java.lang.String
encodeUrlIso(java.lang.String stringToEncode)
URL-encodes a given string using ISO-8859-1, which may work better with web pages and umlauts compared to UTF-8.static java.util.List<java.lang.String>
findLinesContaining(java.lang.String text, java.lang.String searchText)
static java.lang.String
hex(byte[] bytes)
static java.lang.String
join(int[] array, java.lang.String separator)
Joins the given ints using the given separator into a single string.static java.lang.String
join(java.lang.Iterable<?> iterable, java.lang.String separator)
Joins the given iterable objects using the given separator into a single string.static java.lang.String
join(java.lang.String[] array, java.lang.String separator)
Joins the given Strings using the given separator into a single string.static java.lang.String
md5(java.lang.String stringToEncode)
Generates the MD5 digest (32 hex characters) for a given String based on UTF-8.static byte[]
parseHex(java.lang.String hex)
static int
parseHexDigit(char c)
static java.lang.String
resolveEntity(java.lang.String entity)
Simple HTML/XML entity resolving: Only supports unicode entities and a very limited number text represented entities (apos, quot, gt, lt, and amp).static java.lang.String
sha1(java.lang.String stringToEncode)
Generates the SHA-1 digest (40 hex characters) for a given String based on UTF-8.static java.lang.String
sha256(java.lang.String stringToEncode)
Generates the SHA-256 digest (64 hex characters) for a given String based on UTF-8.static java.lang.String[]
split(java.lang.String string, char delimiter)
Splits a String based on a single character, which is usually faster than regex-based String.split().static java.lang.String[]
splitLines(java.lang.String text, boolean skipEmptyLines)
-
-
-
Method Detail
-
split
public static java.lang.String[] split(java.lang.String string, char delimiter)
Splits a String based on a single character, which is usually faster than regex-based String.split(). NOTE: split("AA;BB;;", ';') == ["AA", "BB", "", ""], this may be different from String.split()
-
encodeUrl
public static java.lang.String encodeUrl(java.lang.String stringToEncode)
URL-Encodes a given string using UTF-8 (some web pages have problems with UTF-8 and umlauts, considerencodeUrlIso(String)
also). No UnsupportedEncodingException to handle as it is dealt with in this method.
-
encodeUrlIso
public static java.lang.String encodeUrlIso(java.lang.String stringToEncode)
URL-encodes a given string using ISO-8859-1, which may work better with web pages and umlauts compared to UTF-8. No UnsupportedEncodingException to handle as it is dealt with in this method.
-
decodeUrl
public static java.lang.String decodeUrl(java.lang.String stringToDecode)
URL-Decodes a given string using UTF-8. No UnsupportedEncodingException to handle as it is dealt with in this method.
-
decodeUrlIso
public static java.lang.String decodeUrlIso(java.lang.String stringToDecode)
URL-Decodes a given string using ISO-8859-1. No UnsupportedEncodingException to handle as it is dealt with in this method.
-
md5
public static java.lang.String md5(java.lang.String stringToEncode)
Generates the MD5 digest (32 hex characters) for a given String based on UTF-8.
-
sha1
public static java.lang.String sha1(java.lang.String stringToEncode)
Generates the SHA-1 digest (40 hex characters) for a given String based on UTF-8. The SHA-1 algorithm produces less collisions than MD5.- Returns:
- SHA-1 digest
-
sha256
public static java.lang.String sha256(java.lang.String stringToEncode)
Generates the SHA-256 digest (64 hex characters) for a given String based on UTF-8. The SHA-256 algorithm is less broken than SHA-1.- Returns:
- SHA-256 digest
-
digest
public static java.lang.String digest(java.lang.String string, java.lang.String digestAlgo, java.lang.String encoding)
Generates a digest (hex string) for the given string
-
hex
public static java.lang.String hex(byte[] bytes)
-
parseHex
public static byte[] parseHex(java.lang.String hex)
- Throws:
java.lang.IllegalArgumentException
- if the given string is invalid hex
-
parseHexDigit
public static int parseHexDigit(char c)
- Throws:
java.lang.IllegalArgumentException
- if the given char is invalid hex
-
resolveEntity
public static java.lang.String resolveEntity(java.lang.String entity)
Simple HTML/XML entity resolving: Only supports unicode entities and a very limited number text represented entities (apos, quot, gt, lt, and amp). There are many more: http://www.w3.org/TR/REC-html40/sgml/dtd.html- Parameters:
entity
- The entity name without & and ; (null throws NPE)- Returns:
- Resolved entity or the entity itself if it could not be resolved.
-
ellipsize
public static java.lang.String ellipsize(java.lang.String text, int maxLength)
Cuts the string at the end if it's longer than maxLength and appends "..." to it. The length of the resulting string including "..." is always less or equal to the given maxLength. It's valid to pass a null text; in this case null is returned.
-
ellipsize
public static java.lang.String ellipsize(java.lang.String text, int maxLength, java.lang.String end)
Cuts the string at the end if it's longer than maxLength and appends the given end string to it. The length of the resulting string is always less or equal to the given maxLength. It's valid to pass a null text; in this case null is returned.
-
splitLines
public static java.lang.String[] splitLines(java.lang.String text, boolean skipEmptyLines)
-
findLinesContaining
public static java.util.List<java.lang.String> findLinesContaining(java.lang.String text, java.lang.String searchText)
-
join
public static java.lang.String join(java.lang.Iterable<?> iterable, java.lang.String separator)
Joins the given iterable objects using the given separator into a single string.- Returns:
- the joined string or an empty string if iterable is null
-
join
public static java.lang.String join(int[] array, java.lang.String separator)
Joins the given ints using the given separator into a single string.- Returns:
- the joined string or an empty string if the int array is null
-
join
public static java.lang.String join(java.lang.String[] array, java.lang.String separator)
Joins the given Strings using the given separator into a single string.- Returns:
- the joined string or an empty string if the String array is null
-
-