Class StringUtils

java.lang.Object
org.greenrobot.essentials.StringUtils

public class StringUtils extends Object
Utilities for working with strings like splitting, joining, url-encoding, hex, and digests.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final char[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    decodeUrl(String stringToDecode)
    URL-Decodes a given string using UTF-8.
    static String
    decodeUrlIso(String stringToDecode)
    URL-Decodes a given string using ISO-8859-1.
    static String
    digest(String string, String digestAlgo, String encoding)
    Generates a digest (hex string) for the given string
    static String
    ellipsize(String text, int maxLength)
    Cuts the string at the end if it's longer than maxLength and appends "..." to it.
    static String
    ellipsize(String text, int maxLength, String end)
    Cuts the string at the end if it's longer than maxLength and appends the given end string to it.
    static String
    encodeUrl(String stringToEncode)
    URL-Encodes a given string using UTF-8 (some web pages have problems with UTF-8 and umlauts, consider encodeUrlIso(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.
    static List<String>
    findLinesContaining(String text, String searchText)
     
    static String
    hex(byte[] bytes)
     
    static String
    join(int[] array, String separator)
    Joins the given ints using the given separator into a single string.
    static String
    join(Iterable<?> iterable, String separator)
    Joins the given iterable objects using the given separator into a single string.
    static String
    join(String[] array, String separator)
    Joins the given Strings using the given separator into a single string.
    static String
    md5(String stringToEncode)
    Generates the MD5 digest (32 hex characters) for a given String based on UTF-8.
    static byte[]
     
    static int
    parseHexDigit(char c)
     
    static String
    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
    sha1(String stringToEncode)
    Generates the SHA-1 digest (40 hex characters) for a given String based on UTF-8.
    static String
    sha256(String stringToEncode)
    Generates the SHA-256 digest (64 hex characters) for a given String based on UTF-8.
    static String[]
    split(String string, char delimiter)
    Splits a String based on a single character, which is usually faster than regex-based String.split().
    static String[]
    splitLines(String text, boolean skipEmptyLines)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HEX_CHARS

      private static final char[] HEX_CHARS
  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • split

      public static String[] split(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 String encodeUrl(String stringToEncode)
      URL-Encodes a given string using UTF-8 (some web pages have problems with UTF-8 and umlauts, consider encodeUrlIso(String) also). No UnsupportedEncodingException to handle as it is dealt with in this method.
    • encodeUrlIso

      public 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. No UnsupportedEncodingException to handle as it is dealt with in this method.
    • decodeUrl

      public static String decodeUrl(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 String decodeUrlIso(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 String md5(String stringToEncode)
      Generates the MD5 digest (32 hex characters) for a given String based on UTF-8.
    • sha1

      public static String sha1(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 String sha256(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 String digest(String string, String digestAlgo, String encoding)
      Generates a digest (hex string) for the given string
    • hex

      public static String hex(byte[] bytes)
    • parseHex

      public static byte[] parseHex(String hex)
      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

      public 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). 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

      public static String ellipsize(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 String ellipsize(String text, int maxLength, 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 String[] splitLines(String text, boolean skipEmptyLines)
    • findLinesContaining

      public static List<String> findLinesContaining(String text, String searchText)
    • join

      public static String join(Iterable<?> iterable, 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 String join(int[] array, 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 String join(String[] array, 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