Package org.greenrobot.essentials
Class StringUtils
java.lang.Object
org.greenrobot.essentials.StringUtils
Utilities for working with strings like splitting, joining, url-encoding, hex, and digests.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
URL-Decodes a given string using UTF-8.static String
decodeUrlIso
(String stringToDecode) URL-Decodes a given string using ISO-8859-1.static String
Generates a digest (hex string) for the given stringstatic String
Cuts the string at the end if it's longer than maxLength and appends "..." to it.static String
Cuts the string at the end if it's longer than maxLength and appends the given end string to it.static String
URL-Encodes a given string using UTF-8 (some web pages have problems with UTF-8 and umlauts, considerencodeUrlIso(String)
also).static String
encodeUrlIso
(String stringToEncode) URL-encodes a given string using ISO-8859-1, which may work better with web pages and umlauts compared to UTF-8.findLinesContaining
(String text, String searchText) static String
hex
(byte[] bytes) static String
Joins the given ints using the given separator into a single string.static String
Joins the given iterable objects using the given separator into a single string.static String
Joins the given Strings using the given separator into a single string.static String
Generates the MD5 digest (32 hex characters) for a given String based on UTF-8.static byte[]
static int
parseHexDigit
(char c) static String
resolveEntity
(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 String
Generates the SHA-1 digest (40 hex characters) for a given String based on UTF-8.static String
Generates the SHA-256 digest (64 hex characters) for a given String based on UTF-8.static String[]
Splits a String based on a single character, which is usually faster than regex-based String.split().static String[]
splitLines
(String text, boolean skipEmptyLines)
-
Field Details
-
HEX_CHARS
private static final char[] HEX_CHARS
-
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
split
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
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
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
URL-Decodes a given string using UTF-8. No UnsupportedEncodingException to handle as it is dealt with in this method. -
decodeUrlIso
URL-Decodes a given string using ISO-8859-1. No UnsupportedEncodingException to handle as it is dealt with in this method. -
md5
Generates the MD5 digest (32 hex characters) for a given String based on UTF-8. -
sha1
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
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
Generates a digest (hex string) for the given string -
hex
-
parseHex
- Throws:
IllegalArgumentException
- if the given string is invalid hex
-
parseHexDigit
public static int parseHexDigit(char c) - Throws:
IllegalArgumentException
- if the given char is invalid hex
-
resolveEntity
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 invalid input: '&' and ; (null throws NPE)- Returns:
- Resolved entity or the entity itself if it could not be resolved.
-
ellipsize
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
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
-
findLinesContaining
-
join
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
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
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
-