com.myjavatools.lib
Class Strings

java.lang.Object
  extended bycom.myjavatools.lib.Objects
      extended bycom.myjavatools.lib.Strings
Direct Known Subclasses:
Files

public class Strings
extends Objects


Field Summary
static int _FALSE_
           
static int _TRUE_
          Three "fuzzy logical" values, _TRUE_, _FALSE_, _UNDEF_ ("Intuitionistic" would be a more correct scientific term for these).
static int _UNDEF_
           
static java.lang.String ESCAPED
          Characters used in escapes "\\\"\'nrtfb"
static java.lang.String ESCAPEE
          Characters that should be escaped in Java or C code "\\\"\'\n\r\t\f\b"
 
Method Summary
static java.util.Properties asProperties(java.lang.String[] pairs)
          Create Properties from an array of key-value pairs
static int countChar(java.lang.CharSequence s, char c)
          Counts the number of occurrences of char c in CharSequence s.
static int countLeadingSpaces(java.lang.CharSequence s)
          Counts leading spaces in a char sequence
static int countTrailingSpaces(java.lang.CharSequence s)
          Counts trailing spaces in a char sequence
static long crc32(java.lang.CharSequence data)
          Calculates crc32 on a char sequence
static java.lang.String crcreport(byte[] data, int step)
          Returns a crc report on a byte array: a set of partial crc on chunks of data
static java.lang.String crcreport(byte[] data, int off, int len, int step)
          Returns a crc report on a byte array: a set of partial crc on chunks of data
static java.lang.String decode(byte[] bytes, java.lang.String encoding)
          Decodes an array of bytes using specified encoding
static java.lang.String decode(java.io.InputStream is, java.lang.String encoding)
          Decodes a stream using specified encoding
static java.lang.String decodeJavaString(java.lang.CharSequence string)
          Decodes (and unescapes) a Java string.
static byte[] encode(java.lang.CharSequence s, java.lang.String encoding)
          Encodes a char sequence using specified encoding
static java.lang.String extractValue(java.lang.CharSequence input, java.lang.CharSequence name)
          Extracts value from a char sequence of format NAME="VALUE"
static java.lang.String fill(char c, int n)
          Fills a string with a character
static int findDiff(java.lang.CharSequence s1, java.lang.CharSequence s2)
          Finds index of the first difference between two char sequences
static java.lang.String format(java.lang.String fmtString, java.lang.Object param1)
          Formats string with one parameter
static java.lang.String format(java.lang.String fmtString, java.lang.Object param1, java.lang.Object param2)
          Formats string with two parameters
static java.lang.String format(java.lang.String fmtString, java.lang.Object param1, java.lang.Object param2, java.lang.Object param3)
          Formats string with three parameters
static java.util.List grep(java.lang.CharSequence[] source, java.lang.CharSequence regexp)
          Perl operation grep.
static java.util.List grep(java.lang.CharSequence[] source, java.util.regex.Pattern regexp)
          Perl operation grep.
static boolean hasAlpha(java.lang.CharSequence s)
          Checks whether a CharSequence contains any latin letters.
static java.lang.String hexDump(byte[] data)
          Hexadecimal dump of a byte array.
static java.lang.String hexDump(char[] data)
          Hexadecimal dump of a char array Produces neatly arranged lines of bot hex and ascii representation of bytes from the array.
static java.lang.String hexDump(java.lang.CharSequence data)
          Hexadecimal dump of a CharSequence Produces neatly arranged lines of bot hex and ascii representation of bytes from the array.
static java.lang.String htmlEncode(java.lang.CharSequence s)
          encodes a CharSequence into an HTML-acceptable format
static int indexOf(java.lang.CharSequence s, char c)
          CharSequence version of indexOf
static int indexOf(java.lang.CharSequence s, char c, int fromIndex)
          CharSequence version of indexOf
static int indexOf(java.lang.CharSequence sequence, java.lang.CharSequence subsequence)
          CharSequence version of indexOf
static int indexOf(java.lang.CharSequence sequence, java.lang.CharSequence subsequence, int fromIndex)
          CharSequence version of indexOf
static boolean isAlmostEmpty(java.lang.CharSequence s)
          Checks whether a CharSequence does not contain anything except whitespaces and the like.
static boolean isAlpha(char c)
          Deprecated. use Character.isJavaIdentifierStart(char)

Examples:
  • isAlpha('a'), isAlpha('O'), isAlpha('I'), isAlpha('l') return true;
  • isAlpha('+'), isAlpha('0'), isAlpha('|'), isAlpha('1') return false.
  •  int isTrue(java.lang.String string)
              Extracts logical value from a string
    static boolean isVowel(char c)
              Checks whether a character is a latin vowel.
    static java.lang.String join(java.lang.CharSequence separator, java.util.Collection collection)
              Perl operation join.
    static java.lang.String join(java.lang.CharSequence separator, java.lang.Object[] what)
              Perl operation join.
    static int lastIndexOf(java.lang.CharSequence s, char c)
              CharSequence version of lastIndexOf
    static boolean needsEncoding(char c)
              Checks whether a character needs encoding in Java
    static boolean needsEncoding(java.lang.CharSequence s)
              Checks whether a CharSequence needs encoding in Java
    static java.lang.String oneOf(java.lang.Object o1, java.lang.Object o2)
              Chooses a string representation of a non-empty object out of two objects.
    static java.lang.String oneOf(java.lang.Object o1, java.lang.Object o2, java.lang.Object o3)
              Chooses a string representation of a non-empty object out of three objects.
    static java.lang.String oneOf(java.lang.Object o1, java.lang.Object o2, java.lang.Object o3, java.lang.Object o4)
              Chooses a string representation of a non-empty object out of three objects.
    static java.lang.String pack(byte[] from)
              Packs bytes into a string
    static java.lang.String replace(java.lang.CharSequence where, java.lang.CharSequence oldSubstring, java.lang.CharSequence newSubstring, boolean all)
              Replaces a subsequence in a char sequence with another subsequence
    static java.lang.String sgmlEntity(char c)
              Encodes a character by SGML rules It can be a hex representation
    static java.util.List split(java.lang.CharSequence separator, java.lang.CharSequence source)
              Perl operation split.
    static boolean startsWith(java.lang.CharSequence sequence, java.lang.CharSequence subsequence)
              CharSequence version of startsWith
    static int textHeight(java.lang.CharSequence s)
              Calculates how many lines the text contains.
    static int textWidth(java.lang.CharSequence s)
              Calculates how many horizontal lines will the text take in a textarea.
     boolean toBoolean(java.lang.String string, boolean defaultValue)
              Extracts Boolean value from a string
    static java.lang.String toCEncoding(char c)
              Converts a character to its C encoding (hex or escaped or intact)
    static java.lang.String toCEncoding(java.lang.CharSequence s)
              Converts a CharSequence to its C encoding
    static java.lang.String toHex(byte b)
              Converts a byte to hex string.
    static java.lang.String toHex(char ch)
              Converts a char to hex string
    static java.lang.String toHex(char ch, boolean up)
              Converts a char to hex string
    static java.lang.String toHex(java.lang.CharSequence s, boolean up)
              Converts a CharSequence to hex string (character by character)
    static java.lang.String toHex(int i)
              Converts an integer to hex string.
    static java.lang.String toHexReadable(byte[] data)
              Converts an array of bytes to a readable hexadecimal form
    static java.lang.String toHexReadable(byte[] data, int from, int to)
              Converts an array of chars to a readable hexadecimal form
    static java.lang.String toHexReadable(char[] data)
              Converts an array of chars to a readable hexadecimal form
    static java.lang.String toHexReadable(java.lang.CharSequence s)
              Converts a CharSequence to a readable hexadecimal string
    static java.lang.String toJavaEncoding(char c)
              Converts a character to its Java encoding (hex or escaped or intact)
    static java.lang.String toJavaEncoding(char c, boolean up)
              Converts a character to its Java encoding (hex or escaped or intact)
    static java.lang.String toJavaEncoding(char c, boolean up, boolean escape)
              Converts a character to its Java encoding (hex or escaped or intact)
    static java.lang.String toJavaEncoding(java.lang.CharSequence s)
              Converts a CharSequence to its Java encoding (hex or escaped or intact, per char)
    static java.lang.String toJavaEncoding(java.lang.CharSequence s, boolean up)
              Converts a CharSequence to its Java encoding (hex or escaped or intact, per char)
    static java.lang.String toJavaEncoding(java.lang.CharSequence s, boolean up, boolean escape)
              Converts a CharSequence to its Java encoding (hex or escaped or intact, per char)
    static java.lang.String toJavaHexEncoding(char c)
              Converts a character to its Java hex encoding format: \\uxxxx
    static java.lang.String toJavaHexEncoding(char c, boolean up)
              Converts a character to its Java hex encoding format: \\uxxxx
    static java.lang.String toJavaOctalEncoding(char c)
              Converts a character to its Java octal encoding format: \\o[o][o]
    static java.lang.String toPropertiesEncoding(char c)
              Converts a character to how it should be represented in properties files
    static java.lang.String toPropertiesEncoding(char c, boolean up)
              Converts a character to how it should be represented in properties files
    static java.lang.String toReadable(char[] data, int beginIndex, int endIndex)
              Converts a char array to a readable string.
    static java.lang.String toReadable(java.lang.CharSequence s)
              Converts a CharSequence to a readable string.
    static java.lang.String toSgmlEncoding(char c)
              Converts a character to its SGML numeric encoding
    static java.lang.String toSgmlEncoding(java.lang.CharSequence s)
              Encodes a CharSequence by SGML rules (using predefined entities and numeric character encodings when necessary)
    static java.lang.String toString(java.lang.Throwable e)
              Stringifies a Throwable, together with is stack trace.
    static java.lang.String[] toStrings(java.lang.Object object)
              Converts an array to string array, per element
    static byte[] unpack(java.lang.CharSequence data)
              Unpacks bytes packed in the char sequence
    static java.lang.String unzip(byte[] zippedBytes)
              Unzips an array of bytes.
    static java.lang.String unzip(java.lang.CharSequence zipped)
              Unzips a char sequence Does this: CharSequence -> High Unicode bytes -> unzip -> bytes -> UTF8 -> String
    static java.lang.String unzip(java.io.InputStream zippedStream)
              Unzips a stream.
    static int wordCount(java.lang.CharSequence s)
              Calculates the number of words in the CharSequence.
    static void write(java.io.Writer writer, java.lang.CharSequence cs)
              Writes CharSequence to Writer (Hello, Sun! Ever heard of CharSequence class?)
    static java.lang.String zip(java.lang.CharSequence source)
              zips a char sequence to a string.
    static byte[] zip2bytes(java.lang.CharSequence source)
              zip (like in zip files) a string producing an array of bytes
    static java.lang.String zip8bit(java.lang.CharSequence source)
              zips a char sequence to a string of lower-byte chars.
     
    Methods inherited from class com.myjavatools.lib.Objects
    asMap, cat, compose, crc32, crc32, getList, getMap, getMap, getSet, indexOf, indexOf, indexOf, indexOf, inverse, isEmpty, map, map, map, map, map, reallocate, toBytes, toBytes, toChars, toMap, toMap, toMap, toMap, toSet, union
     
    Methods inherited from class java.lang.Object
    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    ESCAPEE

    public static final java.lang.String ESCAPEE
    Characters that should be escaped in Java or C code "\\\"\'\n\r\t\f\b"

    See Also:
    Constant Field Values

    ESCAPED

    public static final java.lang.String ESCAPED
    Characters used in escapes "\\\"\'nrtfb"

    See Also:
    Constant Field Values

    _TRUE_

    public static final int _TRUE_
    Three "fuzzy logical" values, _TRUE_, _FALSE_, _UNDEF_ ("Intuitionistic" would be a more correct scientific term for these).

    See Also:
    Constant Field Values

    _FALSE_

    public static final int _FALSE_
    See Also:
    Constant Field Values

    _UNDEF_

    public static final int _UNDEF_
    See Also:
    Constant Field Values
    Method Detail

    indexOf

    public static int indexOf(java.lang.CharSequence s,
                              char c,
                              int fromIndex)
    CharSequence version of indexOf

    Parameters:
    s - CharSequence
    c - char
    fromIndex - int
    Returns:
    int see java.lang.String#indexOf() for description

    indexOf

    public static int indexOf(java.lang.CharSequence s,
                              char c)
    CharSequence version of indexOf

    Parameters:
    s - CharSequence
    c - char
    Returns:
    int see java.lang.String.indexOf(char) for description

    lastIndexOf

    public static int lastIndexOf(java.lang.CharSequence s,
                                  char c)
    CharSequence version of lastIndexOf

    Parameters:
    s - CharSequence
    c - char
    Returns:
    int see java.lang.String.lastIndexOf(char) for description

    indexOf

    public static int indexOf(java.lang.CharSequence sequence,
                              java.lang.CharSequence subsequence,
                              int fromIndex)
    CharSequence version of indexOf

    Parameters:
    sequence - CharSequence
    subsequence - CharSequence
    fromIndex - int
    Returns:
    int see java.lang.String.indexOf(String) for description

    indexOf

    public static int indexOf(java.lang.CharSequence sequence,
                              java.lang.CharSequence subsequence)
    CharSequence version of indexOf

    Parameters:
    sequence - CharSequence
    subsequence - CharSequence
    Returns:
    int see java.lang.String.indexOf(String) for description

    startsWith

    public static boolean startsWith(java.lang.CharSequence sequence,
                                     java.lang.CharSequence subsequence)
    CharSequence version of startsWith

    Parameters:
    sequence - CharSequence
    subsequence - CharSequence
    Returns:
    boolean see String.starstWith(String) for description

    write

    public static void write(java.io.Writer writer,
                             java.lang.CharSequence cs)
                      throws java.io.IOException
    Writes CharSequence to Writer (Hello, Sun! Ever heard of CharSequence class?)

    Parameters:
    writer - Writer
    cs - CharSequence
    Throws:
    java.io.IOException

    isAlmostEmpty

    public static boolean isAlmostEmpty(java.lang.CharSequence s)
    Checks whether a CharSequence does not contain anything except whitespaces and the like.

    Parameters:
    s - the sequence to check
    Returns:
    true if empty

    Examples:
  • isAlmostEmpty(""), isAlmostEmpty(null), isAlmostEmpty("\n \r \n") all return true;
  • isAlmostEmpty("."), isAlmostEmpty("Contains data!") returns false.

  • oneOf

    public static java.lang.String oneOf(java.lang.Object o1,
                                         java.lang.Object o2)
    Chooses a string representation of a non-empty object out of two objects.

    Parameters:
    o1 - the first candidate
    o2 - the second candidate
    Returns:
    the first one that is not empty, converted to String

    Examples:
  • oneOf(null, "xyz") returns "xyz";
  • oneOf("abc", "xyz") returns "abc";
  • oneOf("", null) return null;
  • oneOf("abc", null) returns "abc".

  • oneOf

    public static java.lang.String oneOf(java.lang.Object o1,
                                         java.lang.Object o2,
                                         java.lang.Object o3)
    Chooses a string representation of a non-empty object out of three objects.

    Parameters:
    o1 - the first candidate
    o2 - the second candidate
    o3 - the third candidate
    Returns:
    the string representation of the first one that is not empty

    Examples:
  • oneOf(null, "", "xyz") returns "xyz";
  • oneOf("abc", null, "xyz") returns "abc";
  • oneOf("", "def", null) returns "def";
  • oneOf("", null, "") returns "".

  • oneOf

    public static java.lang.String oneOf(java.lang.Object o1,
                                         java.lang.Object o2,
                                         java.lang.Object o3,
                                         java.lang.Object o4)
    Chooses a string representation of a non-empty object out of three objects.

    Parameters:
    o1 - the first candidate
    o2 - the second candidate
    o3 - the third candidate
    o4 - the fourth candidate
    Returns:
    the string representation of the first one that is not empty

    Examples:
  • oneOf(null, "", null, "xyz") returns "xyz";
  • oneOf("abc", null, "pqr", "xyz") returns "abc";
  • oneOf("", "def", null, "xyz") returns "def";
  • oneOf("", null, "", null) returns null.

  • isAlpha

    public static boolean isAlpha(char c)
    Deprecated. use Character.isJavaIdentifierStart(char)

    Examples:
  • isAlpha('a'), isAlpha('O'), isAlpha('I'), isAlpha('l') return true;
  • isAlpha('+'), isAlpha('0'), isAlpha('|'), isAlpha('1') return false.
  • Checks whether a character is a latin letter.

    Parameters:
    c - character to check
    Returns:
    true if it is so

    isVowel

    public static boolean isVowel(char c)
    Checks whether a character is a latin vowel.

    Parameters:
    c - the char to tests
    Returns:
    true if the character is one of "aeiaouAEIAOU"

    hasAlpha

    public static boolean hasAlpha(java.lang.CharSequence s)
    Checks whether a CharSequence contains any latin letters.

    Parameters:
    s - CharSequence to check
    Returns:
    true if it is so

    Examples:
  • hasAlpha("a"), hasAlpha("2OO2"), hasAlpha("This is a string") return true;
  • hasAlpha("+"), hasAlpha("1900"), hasAlpha("|1!*") return false.

  • countChar

    public static int countChar(java.lang.CharSequence s,
                                char c)
    Counts the number of occurrences of char c in CharSequence s.

    Parameters:
    s - the string to scan
    c - the character to count
    Returns:
    the number of occurrences

    Example:
  • countChar("Goodness me, the clock has struck", 'o') returns 3.

  • textHeight

    public static int textHeight(java.lang.CharSequence s)
    Calculates how many lines the text contains. Lines are supposed to be separated by '\n' character.

    Parameters:
    s - the CharSequence with text
    Returns:
    number of lines in the string (separated by '\n')

    Examples:
  • textHeight("One\nTwo\nThree") returns 3;
  • textHeight("\nOne\nTwo\nThree\n") returns 5.

  • textWidth

    public static int textWidth(java.lang.CharSequence s)
    Calculates how many horizontal lines will the text take in a textarea. This is the maximum line length for all lines in the text. Lines are separated by '\n' character.

    Parameters:
    s - the CharSequence with text
    Returns:
    maximum line length in the text

    Example:
  • textWidth("One\nTwo\nThree") returns 5.

  • wordCount

    public static int wordCount(java.lang.CharSequence s)
    Calculates the number of words in the CharSequence. This is just the number of tokens separated by default separators.

    Parameters:
    s - the CharSequence to analyze
    Returns:
    number of words

    Examples:
  • wordCount("This is life!") returns 3;
  • wordCount("C'est la vie !") returns 4, but for a wrong reason.

  • countLeadingSpaces

    public static int countLeadingSpaces(java.lang.CharSequence s)
    Counts leading spaces in a char sequence

    Parameters:
    s -
    Returns:
    number of leading spaces

    Example:
  • countLeadingSpaces(" this is a string ") returns 1.

  • countTrailingSpaces

    public static int countTrailingSpaces(java.lang.CharSequence s)
    Counts trailing spaces in a char sequence

    Parameters:
    s -
    Returns:
    number of trailing spaces

    Example:
  • countTrailingSpaces(" this is a string ") returns 3.

  • fill

    public static java.lang.String fill(char c,
                                        int n)
    Fills a string with a character

    Parameters:
    c -
    n -
    Returns:
    a new string consisting of character c repeated n times

    Example:
  • fill("*", 10) returns "**********".

  • toHex

    public static java.lang.String toHex(byte b)
    Converts a byte to hex string.

    Parameters:
    b - the byte
    Returns:
    b representation as a two-character hex string

    Example:
  • toHex(155) returns "9b".

  • toHex

    public static java.lang.String toHex(int i)
    Converts an integer to hex string. It is the same as Integer.toHexString().

    Parameters:
    i - the integer
    Returns:
    i representation as a hex string

    Example:
  • toHex(1234) returns "4d2".

  • toHex

    public static java.lang.String toHex(char ch,
                                         boolean up)
    Converts a char to hex string

    Parameters:
    ch - the char
    up - if true, use upper case, otherwise lower
    Returns:
    i representation as a four-character hex string

    Examples:
  • toHex('?', true) returns "12BC";
  • toHex('', false) returns "00af".

  • toHex

    public static java.lang.String toHex(char ch)
    Converts a char to hex string

    Parameters:
    ch - the char
    Returns:
    a four-character string (lower case)

    Example:
  • toHex('') returns "00af".

  • toHex

    public static java.lang.String toHex(java.lang.CharSequence s,
                                         boolean up)
    Converts a CharSequence to hex string (character by character)

    Parameters:
    s - the CharSequence
    up - if true, use upper case, otherwise lower
    Returns:
    s representation as a hex string

    Examples:
  • toHex("kl?", true) returns "006B006C12BC";
  • toHex("kl?", true) returns "006b006c12bc".

  • toJavaOctalEncoding

    public static java.lang.String toJavaOctalEncoding(char c)
    Converts a character to its Java octal encoding format: \\o[o][o]

    Parameters:
    c - the character
    Returns:
    a string

    Example:
  • toJavaOctalEncoding('\n') returns "\\12".

  • toJavaHexEncoding

    public static java.lang.String toJavaHexEncoding(char c)
    Converts a character to its Java hex encoding format: \\uxxxx

    Parameters:
    c - the character
    Returns:
    a string, encoded c representation

    Example:
  • toJavaHexEncoding('') returns "\\u00af".

  • toJavaHexEncoding

    public static java.lang.String toJavaHexEncoding(char c,
                                                     boolean up)
    Converts a character to its Java hex encoding format: \\uxxxx

    Parameters:
    c - the character
    up - if true, use upper case, otherwise lower
    Returns:
    a string, encoded c representation

    Example:
  • toJavaHexEncoding('', false) returns "\\u00af".

  • toPropertiesEncoding

    public static java.lang.String toPropertiesEncoding(char c,
                                                        boolean up)
    Converts a character to how it should be represented in properties files

    Parameters:
    c - the character
    up - if true, use upper case, otherwise lower
    Returns:
    a string, encoded c representation

    Examples:
  • toPropertiesEncoding('', false) returns "\\u00af";
  • toPropertiesEncoding('', true) returns "\\u00AF";
  • toPropertiesEncoding('a', false) returns "a".

  • toPropertiesEncoding

    public static java.lang.String toPropertiesEncoding(char c)
    Converts a character to how it should be represented in properties files

    Parameters:
    c - the character
    Returns:
    a string, encoded c representation

    Examples:
  • toPropertiesEncoding('') returns "\\u00af";
  • toPropertiesEncoding('a') returns "a".

  • needsEncoding

    public static boolean needsEncoding(char c)
    Checks whether a character needs encoding in Java

    Parameters:
    c - the character
    Returns:
    true if so

    Examples:
  • needsEncoding('') returns true;
  • needsEncoding('a') returns false.

  • toJavaEncoding

    public static java.lang.String toJavaEncoding(char c,
                                                  boolean up,
                                                  boolean escape)
    Converts a character to its Java encoding (hex or escaped or intact)

    Parameters:
    c - the character
    up - if true, use upper case, otherwise lower
    escape - if true, escape escapable characters
    Returns:
    a string with a proper Java representation of the character c

    Examples:
  • toJavaEncoding('', false, false) returns "\\u00af";
  • toJavaEncoding(' ', true, true) returns "\\n";
  • toJavaEncoding('', true, true) returns "\\16";
  • toJavaEncoding('a', true, true) returns "a".

  • toJavaEncoding

    public static java.lang.String toJavaEncoding(char c,
                                                  boolean up)
    Converts a character to its Java encoding (hex or escaped or intact)

    Parameters:
    c - the character
    up - if true, use upper case, otherwise lower
    Returns:
    a string with a proper Java representation of the character c

    Examples:
  • toJavaEncoding('', false) returns "\\u00af";
  • toJavaEncoding('', true) returns "\\u00AF";
  • toJavaEncoding(' ', true) returns "\\n";
  • toJavaEncoding('', true) returns "\\16";
  • toJavaEncoding('a', true) returns "a".

  • toJavaEncoding

    public static java.lang.String toJavaEncoding(char c)
    Converts a character to its Java encoding (hex or escaped or intact)

    Parameters:
    c - the character
    Returns:
    a string with a proper Java representation of the character c

    Examples:
  • toJavaEncoding('') returns "\\u00af";
  • toJavaEncoding(' ') returns "\\n";
  • toJavaEncoding('') returns "\\16";
  • toJavaEncoding('a') returns "a".

  • toCEncoding

    public static java.lang.String toCEncoding(char c)
    Converts a character to its C encoding (hex or escaped or intact)

    Parameters:
    c - the character
    Returns:
    a string with a proper C language representation of the character c

    Examples:
  • toCEncoding('?') returns "\\xabcd";
  • toCEncoding('') returns "\\xaf";
  • toCEncoding(' ') returns "\\n";
  • toCEncoding('a') returns "a".

  • needsEncoding

    public static boolean needsEncoding(java.lang.CharSequence s)
    Checks whether a CharSequence needs encoding in Java

    Parameters:
    s - the CharSequence
    Returns:
    true if so

    Examples:
  • needsEncoding("Feliz Ao Nuevo") returns true;
  • needsEncoding("Feliz Navedad") returns false.

  • toJavaEncoding

    public static java.lang.String toJavaEncoding(java.lang.CharSequence s,
                                                  boolean up,
                                                  boolean escape)
    Converts a CharSequence to its Java encoding (hex or escaped or intact, per char)

    Parameters:
    s - the CharSequence
    up - if true, use upper case, otherwise lower
    escape - if true, escape escapable characters
    Returns:
    the encoded string

    Examples:
  • toJavaEncoding("\nFeliz Ao Nuevo\n", true, false) returns " Feliz o Nuevo ";
  • toJavaEncoding("\nFeliz Ao Nuevo\n", true, true) returns "\\nFeliz o Nuevo\\n";
  • toJavaEncoding("\nFeliz Ao Nuevo\n\0", false, true) returns "\\nFeliz o Nuevo\\n\\0".

  • toJavaEncoding

    public static java.lang.String toJavaEncoding(java.lang.CharSequence s,
                                                  boolean up)
    Converts a CharSequence to its Java encoding (hex or escaped or intact, per char)

    Parameters:
    s - the CharSequence
    up - if true, use upper case, otherwise lower
    Returns:
    the encoded string

    Examples:
  • toJavaEncoding("\nFeliz Ao Nuevo\n", true) returns "\nFeliz o Nuevo\n";
  • toJavaEncoding("\nFeliz Ao Nuevo\n\0", false) returns "\\nFeliz o Nuevo\\n\\0".

  • toJavaEncoding

    public static java.lang.String toJavaEncoding(java.lang.CharSequence s)
    Converts a CharSequence to its Java encoding (hex or escaped or intact, per char)

    Parameters:
    s - the CharSequence
    Returns:
    the encoded string

    Example:
  • toJavaEncoding("\nFeliz Ao Nuevo\n\0") returns "\\nFeliz Ao Nuevo\\n\\0".

  • toCEncoding

    public static java.lang.String toCEncoding(java.lang.CharSequence s)
    Converts a CharSequence to its C encoding

    Parameters:
    s - the CharSequence
    Returns:
    the encoded string

    Example:
  • toCEncoding("\nFeliz Ao Nuevo\n") returns "\\nFeliz A\\x00f1o Nuevo\\n".

  • toSgmlEncoding

    public static java.lang.String toSgmlEncoding(char c)
    Converts a character to its SGML numeric encoding

    Parameters:
    c - the character
    Returns:
    a string with the representation of c as "Numeric Character Reference" in SGMLese

    Example:
  • toSgmlEncoding('\n') returns "
".

  • sgmlEntity

    public static java.lang.String sgmlEntity(char c)
    Encodes a character by SGML rules It can be a hex representation

    Parameters:
    c - the character
    Returns:
    the string with either Predefined Entity, Numeric Character Reference, or null if no entity could be found

    Examples:
  • sgmlEntity('\60ab') returns "悫" (that is, Numeric Character Reference);
  • sgmlEntity('<') returns "&lt;" (that is, Predefined Entity);
  • sgmlEntity('&') returns "&lt;" (that is, Predefined Entity);
  • sgmlEntity('X') returns null";
  • sgmlEntity('\n') returns null".

  • toSgmlEncoding

    public static java.lang.String toSgmlEncoding(java.lang.CharSequence s)
    Encodes a CharSequence by SGML rules (using predefined entities and numeric character encodings when necessary)

    Parameters:
    s - the original CharSequence
    Returns:
    the encoded string

    Example:
  • toSgmlEncoding("Feliz Ao Nuevo\n") returns "&lt;i&gt;Feliz A&#164;o Nuevo&lt;/i&gt;\n".

  • htmlEncode

    public static java.lang.String htmlEncode(java.lang.CharSequence s)
    encodes a CharSequence into an HTML-acceptable format

    Parameters:
    s - CharSequence original CharSequence
    Returns:
    String encoded string All non-ascii characters are replaced with their &# representation; other characters are left intact.

    Example:
  • htmlEncode("Feliz Ao Nuevo\n") returns "Feliz A&#164;o Nuevo\n".

  • toReadable

    public static java.lang.String toReadable(char[] data,
                                              int beginIndex,
                                              int endIndex)
    Converts a char array to a readable string. (By Replacing potentially unreadables characters with '.')

    Parameters:
    data - original char array
    beginIndex - where to start
    endIndex - where to end (before this position)
    Returns:
    the string with unreadable chars replaced

    Example:
  • toReadable("\tHola seor!\n".toCharArray(), 2, 12) will return "..Hola se.or!.".

  • toReadable

    public static java.lang.String toReadable(java.lang.CharSequence s)
    Converts a CharSequence to a readable string. (By Replacing potentially unreadables characters with '.')

    Parameters:
    s - original CharSequence
    Returns:
    the string with unreadable chars replaced

    Example:
  • toReadable("\tHola seor!\n") will return "..Hola se.or!.".

  • hexDump

    public static java.lang.String hexDump(byte[] data)
    Hexadecimal dump of a byte array. Produces neatly arranged lines of bot hex and ascii representation of bytes from the array.

    Parameters:
    data - the data array
    Returns:
    the hex dump string

    Example:
  • hexDump(new byte[] {1, 'a', 'b', '\n', 'c'}) will return "\r\n01 61 62 0a 63 | . a b . c\r\n".

  • hexDump

    public static java.lang.String hexDump(char[] data)
    Hexadecimal dump of a char array Produces neatly arranged lines of bot hex and ascii representation of bytes from the array.

    Parameters:
    data - the data array
    Returns:
    a string containing both hex and ascii representation of the data

    Example:
  • hexDump(new char[] {1, 'a', 'b', '\n', 'c'}) will return
    "\r\n0000: 0001 0061 0062 000a 0063 | .ab.c\r\n".

  • hexDump

    public static java.lang.String hexDump(java.lang.CharSequence data)
    Hexadecimal dump of a CharSequence Produces neatly arranged lines of bot hex and ascii representation of bytes from the array.

    Parameters:
    data - the CharSequence
    Returns:
    a string containing both hex and ascii representation of the data

    Example:
  • hexDump("ab\nc") will return "\r\n0001 0061 0062 000a 0063 | .ab.c".

  • toHexReadable

    public static java.lang.String toHexReadable(char[] data)
    Converts an array of chars to a readable hexadecimal form

    Parameters:
    data - the data array
    Returns:
    a string that is basically a hex dump of the data

    Example:
  • toHexReadable(new char[] {1, 'a', 'b', '\n', 'c'}) will return "0001 0061 0062 000a 0063 \r\n".

  • toHexReadable

    public static java.lang.String toHexReadable(byte[] data,
                                                 int from,
                                                 int to)
    Converts an array of chars to a readable hexadecimal form

    Parameters:
    data - the data array
    from - beginning index
    to - ending index (not included)
    Returns:
    a string that is basically a hex dump of the data

    Example:
  • toHexReadable(new byte[] {1, 2, 48}, 1, 3) will return "02 30 \r\n".

  • toHexReadable

    public static java.lang.String toHexReadable(byte[] data)
    Converts an array of bytes to a readable hexadecimal form

    Parameters:
    data - the data array
    Returns:
    a string that is basically a hex dump of the data

    Example:
  • toHexReadable(new byte[] {1, 2, 48}) will return "01 02 30 ".

  • toHexReadable

    public static java.lang.String toHexReadable(java.lang.CharSequence s)
    Converts a CharSequence to a readable hexadecimal string

    Parameters:
    s - the data CharSequence
    Returns:
    a string that is basically a hex dump of the data

    Example:
  • toHexReadable("ab\nc") will return "0001 0061 0062 000a 0063 \r\n".

  • join

    public static java.lang.String join(java.lang.CharSequence separator,
                                        java.util.Collection collection)
    Perl operation join. Concatenates a collection of (string representations of) objects using a separator string to separate

    Parameters:
    separator - the separator CharSequence * @param collection the collection of objects to join
    Returns:
    resulting string

    Examples:
    HashSet a = new HashSet();
    List b = new ArrayList(); b.add("entry1"); b.add("entry2");
  • join(", ", a) returns "";
  • join(", ", b) returns "entry1, entry2".

  • join

    public static java.lang.String join(java.lang.CharSequence separator,
                                        java.lang.Object[] what)
    Perl operation join. Concatenates an array of (string representations of) objects using a separator to separate

    Parameters:
    separator - the separator
    what - the array of objects to join
    Returns:
    resulting string

    Examples:
  • join(", ", new Long[] {new Long(1), new Long(555)}) returns "1, 555";
  • join(" and ", new String[] {"Here", "there", "everywhere"}) returns "Here and there and everywhere".

  • split

    public static java.util.List split(java.lang.CharSequence separator,
                                       java.lang.CharSequence source)
    Perl operation split. splits a source string into a collection of strings

    Parameters:
    separator - separator character sequence
    source - source character sequence
    Returns:
    a collection of extracted character sequencess
    See Also:
    see java.lang.String.split(String)

    Example:
  • split(":", "a:ab:abcde:") returns a list containing four elements, "a", "ab", "abcde", "".

  • grep

    public static java.util.List grep(java.lang.CharSequence[] source,
                                      java.lang.CharSequence regexp)
                               throws java.util.regex.PatternSyntaxException
    Perl operation grep. Extracts from a string array the strings that match a regexp

    Parameters:
    source - source array
    regexp - expression to match
    Returns:
    a collection of matching character sequences from source

    Example:
  • grep(new String[] {"good", "bad", "ugly"}, "g.")) returns a list containing two elements: "good", "ugly".
  • Throws:
    java.util.regex.PatternSyntaxException

    grep

    public static java.util.List grep(java.lang.CharSequence[] source,
                                      java.util.regex.Pattern regexp)
    Perl operation grep. Extracts from an array the char sequences that match a regexp

    Parameters:
    source - source array
    regexp - expression to match
    Returns:
    a collection of matching character sequences

    Example:
  • grep(new String[] {"good", "bad", "ugly"}, Pattern.compile("g."))) returns a list containing two elements: "good", "ugly".

  • replace

    public static java.lang.String replace(java.lang.CharSequence where,
                                           java.lang.CharSequence oldSubstring,
                                           java.lang.CharSequence newSubstring,
                                           boolean all)
    Replaces a subsequence in a char sequence with another subsequence

    Parameters:
    where - the string containing the substrings to replace
    oldSubstring - what to replace
    newSubstring - with what to replace
    all - if true, all (nonintersecting) substrings are replaced, otherwise only one
    Returns:
    resulting string see java.lang.String.replaceAll(String,String) and java.lang.String.replaceFirst(String, String)

    Examples:
  • replace("God loves you", "love", "hate", true) returns "God hates you";
  • replace("All you need is love, love!", "me", false) returns "All you need is me, love!".

  • extractValue

    public static java.lang.String extractValue(java.lang.CharSequence input,
                                                java.lang.CharSequence name)
    Extracts value from a char sequence of format NAME="VALUE"

    Parameters:
    input - sequence of the aforementioned format
    name - the name on the left side of '='
    Returns:
    the string value inside the quotes (quotes omitted) if the string has the specified format; null utherwise

    Examples:
  • extractValue("java.home=\"c:\\java\\jdk1.4.1\"\nx=\"abcd\"", "x") returns "abcd";
  • extractValue("java.home=\|c:\\java\\jdk1.4.1\"\nx=\"abcd\"", "java.home") returns "c:\\java\\jdk1.4.1".

  • pack

    public static java.lang.String pack(byte[] from)
    Packs bytes into a string

    Parameters:
    from - byte array
    Returns:
    a string that consists of the same bytes, packed two per character

    Example:
  • pack(new byte[] {0x23, 0x67, (byte)0xab, (byte)0xef}) returns "??".

  • unpack

    public static byte[] unpack(java.lang.CharSequence data)
    Unpacks bytes packed in the char sequence

    Parameters:
    data - the packed data
    Returns:
    the unpacked data

    Example:
  • unpack("??") returns new byte[] {0x23, 0x67, (byte)0xab, (byte)0xef}.
  • See Also:
    pack(byte[])

    decodeJavaString

    public static java.lang.String decodeJavaString(java.lang.CharSequence string)
    Decodes (and unescapes) a Java string. Replaces escape sequences with their corresponding character values

    Parameters:
    string - as presented in the source code
    Returns:
    decoded string in "internal form"

    Examples:
  • decodeJavaString("This is a string") returns "This is a string";
  • decodeJavaString("\\nFeliz \\u00A4o Nuevo\\n") returns "\nFeliz Ao Nuevo\n".

  • encode

    public static byte[] encode(java.lang.CharSequence s,
                                java.lang.String encoding)
                         throws java.io.IOException,
                                java.io.UnsupportedEncodingException
    Encodes a char sequence using specified encoding

    Parameters:
    s - char sequence to encode
    encoding - the name of encoding
    Returns:
    encoded array of bytes
    Throws:
    java.io.IOException - when something goes wrong with bytearray streams
    java.io.UnsupportedEncodingException - when encoding is unknown

    Examples:
  • encode("Ao Nuevo", "UTF8") returns new byte[] {0x41, (byte)0xc3, (byte)0xb1, 0x6f, 0x20, 0x4e, 0x75, 0x65, 0x76, 0x6f};
  • encode("Ao Nuevo", "MacRoman") returns new byte[] {0x41, (byte)0x96, 0x6f, 0x20, 0x4e, 0x75, 0x65, 0x76, 0x6f}.
  • See Also:
    list of Java encodings

    decode

    public static java.lang.String decode(java.io.InputStream is,
                                          java.lang.String encoding)
                                   throws java.io.IOException,
                                          java.io.UnsupportedEncodingException
    Decodes a stream using specified encoding

    Parameters:
    is - stream to decode
    encoding - the name of encoding
    Returns:
    data decoded from the stream
    Throws:
    java.io.IOException - when something goes wrong with bytearray streams
    java.io.UnsupportedEncodingException - when encoding is unknown
    See Also:
    list of Java encodings

    decode

    public static java.lang.String decode(byte[] bytes,
                                          java.lang.String encoding)
                                   throws java.io.IOException,
                                          java.io.UnsupportedEncodingException
    Decodes an array of bytes using specified encoding

    Parameters:
    bytes - byte array
    encoding - the name of encoding
    Returns:
    decoded string
    Throws:
    java.io.IOException - when something goes wrong with bytearray streams
    java.io.UnsupportedEncodingException - when encoding is unknown

    Examples:
  • decode(new byte[] {0x41, (byte)0xc3, (byte)0xb1, 0x6f, 0x20, 0x4e, 0x75, 0x65, 0x76, 0x6f}, "UTF8") returns "Ao Nuevo";
  • encode( new byte[] {0x41, (byte)0x96, 0x6f, 0x20, 0x4e, 0x75, 0x65, 0x76, 0x6f}, "MacRoman") returns "Ao Nuevo".
  • See Also:
    list of Java encodings

    zip2bytes

    public static byte[] zip2bytes(java.lang.CharSequence source)
                            throws java.io.IOException,
                                   java.io.UnsupportedEncodingException
    zip (like in zip files) a string producing an array of bytes

    Parameters:
    source - the string to zip
    Returns:
    bytes representing zipped data
    Throws:
    java.io.IOException - when something goes wrong with streams
    java.io.UnsupportedEncodingException - when JDK forgets that it knows UTF8

    Example:
  • zip2bytes("Hello World") returns new byte[] {0x78, (byte)0xda, (byte)0xf3, 0x48, (byte)0xcd, (byte)0xc9, (byte)0xc9, 0x57, (byte)0x08, (byte)0xcf, 0x2f, (byte)0xca, 0x49, 0x01, 0x00, 0x18, 0x0b, 0x04, 0x1d, 0x00}.

  • zip8bit

    public static java.lang.String zip8bit(java.lang.CharSequence source)
                                    throws java.io.IOException,
                                           java.io.UnsupportedEncodingException
    zips a char sequence to a string of lower-byte chars. Does this: CharSequence -> UTF8 bytes -> zip -> bytes -> String

    Parameters:
    source - char sequence to zip
    Returns:
    string with zipped data
    Throws:
    java.io.IOException - when something goes wrong with streams
    java.io.UnsupportedEncodingException - when JDK forgets that it knows UTF8

    Example:
  • zip8bit("Hello World") returns "xHW\b/I ".

  • zip

    public static java.lang.String zip(java.lang.CharSequence source)
                                throws java.io.IOException,
                                       java.io.UnsupportedEncodingException
    zips a char sequence to a string. Does this: CharSequence -> UTF8 bytes -> zip -> bytes -> High Unicode -> String

    Parameters:
    source - char sequence to zip
    Returns:
    string with zipped data
    Throws:
    java.io.IOException - when something goes wrong with streams
    java.io.UnsupportedEncodingException - when JDK forgets that it knows UTF8

    Example:
  • zip("Hello World") returns "?????????".

  • unzip

    public static java.lang.String unzip(java.io.InputStream zippedStream)
                                  throws java.io.IOException,
                                         java.io.UnsupportedEncodingException
    Unzips a stream. Does this: stream -> unzip -> bytes -> UTF8 -> String

    Parameters:
    zippedStream -
    Returns:
    string with unzipped data
    Throws:
    java.io.IOException - when something goes fishy
    java.io.UnsupportedEncodingException - when JDK forgets that it knows UTF8

    unzip

    public static java.lang.String unzip(byte[] zippedBytes)
                                  throws java.io.IOException,
                                         java.io.UnsupportedEncodingException
    Unzips an array of bytes. Does this: bytes -> unzip -> bytes -> UTF8 -> String

    Parameters:
    zippedBytes -
    Returns:
    string with unzipped data
    Throws:
    java.io.IOException - when something goes fishy
    java.io.UnsupportedEncodingException - when JDK forgets that it knows UTF8

    Example:
  • unzip(new byte[] {0x78, (byte)0xda, (byte)0xf3, 0x48, (byte)0xcd, (byte)0xc9, (byte)0xc9, 0x57, 0x08, (byte)0xcf, 0x2f, (byte)0xca, 0x49, 0x01, 0x00, 0x18, 0x0b, 0x04, 0x1d, 0x00}) returns "Hello World".

  • unzip

    public static java.lang.String unzip(java.lang.CharSequence zipped)
                                  throws java.io.IOException,
                                         java.io.UnsupportedEncodingException
    Unzips a char sequence Does this: CharSequence -> High Unicode bytes -> unzip -> bytes -> UTF8 -> String

    Parameters:
    zipped -
    Returns:
    string with unzipped data
    Throws:
    java.io.IOException - when something goes fishy
    java.io.UnsupportedEncodingException - when JDK forgets that it knows UTF8

    Example:
  • unzip("?????????") returns "Hello World".

  • crc32

    public static long crc32(java.lang.CharSequence data)
                      throws java.io.IOException,
                             java.io.UnsupportedEncodingException
    Calculates crc32 on a char sequence

    Parameters:
    data - source char sequence
    Returns:
    its crc32
    Throws:
    java.io.IOException - when something goes wrong with streams
    java.io.UnsupportedEncodingException - when JDK forgets that it knows UTF8

    Example:
  • crc32("Hello World") returns 2178450716l.

  • crcreport

    public static java.lang.String crcreport(byte[] data,
                                             int off,
                                             int len,
                                             int step)
    Returns a crc report on a byte array: a set of partial crc on chunks of data

    Parameters:
    data - source bytes
    off - offset in the array
    len - length of the area to crc
    step - chunk size
    Returns:
    formatted report

    crcreport

    public static java.lang.String crcreport(byte[] data,
                                             int step)
    Returns a crc report on a byte array: a set of partial crc on chunks of data

    Parameters:
    data - source bytes
    step - chunk size
    Returns:
    formatted report

    toStrings

    public static java.lang.String[] toStrings(java.lang.Object object)
    Converts an array to string array, per element

    Parameters:
    object - the expected array
    Returns:
    array of strings, or null if input is not what expected

    Example:
  • toStrings(new Object[] { new Integer(22), new Boolean(false), "wow"}) returns new String[] {"22", "false", "wow"}.

  • toString

    public static java.lang.String toString(java.lang.Throwable e)
    Stringifies a Throwable, together with is stack trace.

    Parameters:
    e - the throwable to convert to string
    Returns:
    the string representation

    Example:

    try {
      String s = null;
      s.toString();
    } catch (Exception e) {
      System.out.println(toString(e));
    }
    prints

    java.lang.NullPointerException
            at com.myjavatools.util.TestStrings


    format

    public static java.lang.String format(java.lang.String fmtString,
                                          java.lang.Object param1)
    Formats string with one parameter

    Parameters:
    fmtString -
    param1 -
    Returns:
    formatted string

    Example:
  • format("{0} Monkeys", new Long(12)) returns "12 Monkeys".

  • format

    public static java.lang.String format(java.lang.String fmtString,
                                          java.lang.Object param1,
                                          java.lang.Object param2)
    Formats string with two parameters

    Parameters:
    fmtString -
    param1 -
    param2 -
    Returns:
    formatted string

    Example:
  • format("{0} is {1}", "Life", "struggle") returns "Life is struggle".

  • format

    public static java.lang.String format(java.lang.String fmtString,
                                          java.lang.Object param1,
                                          java.lang.Object param2,
                                          java.lang.Object param3)
    Formats string with three parameters

    Parameters:
    fmtString -
    param1 -
    param2 -
    param3 -
    Returns:
    formatted string

    Example:
  • format("{0} + {1} = {2}", new Byte(2), new Byte(2), new Long(5)) returns "2 + 2 = 5".

  • asProperties

    public static java.util.Properties asProperties(java.lang.String[] pairs)
    Create Properties from an array of key-value pairs

    Parameters:
    pairs - the source array
    Returns:
    new Properties

    Example:
  • asProperties(new String[] {"1", "one", "2", "two", "3", "three"}) returns properties with three keys ("1", "2", "3"), and guess which values.

  • findDiff

    public static int findDiff(java.lang.CharSequence s1,
                               java.lang.CharSequence s2)
    Finds index of the first difference between two char sequences

    Parameters:
    s1 -
    s2 -
    Returns:
    the first index at which sequences differ, -1 if the sequences are equal

    Examples:
  • findDiff("abcd", "abec") returns 2;
  • findDiff("abc", "abc") returns -1;
  • findDiff("ab", null) returns 0.
  • findDiff("", " ") returns 0.

  • isTrue

    public int isTrue(java.lang.String string)
    Extracts logical value from a string

    Parameters:
    string -
    Returns:
    _TRUE_ if it is 'true', 'yes', '1' or '+' (case-insensitive), _FALSE_ if it is 'false', 'no', '-' or '-' (case-insensitive), _UNDEF_ in all other cases.

    Examples:
  • isTrue("YeS") returns _TRUE_;
  • isTrue("false") returns _FALSE_;
  • isTrue(null) returns _UNDEF_.

  • toBoolean

    public boolean toBoolean(java.lang.String string,
                             boolean defaultValue)
    Extracts Boolean value from a string

    Parameters:
    string -
    defaultValue -
    Returns:
    true if it is 'true', 'yes', '1' or '+' (case-insensitive), false if it is 'false', 'no', '-' or '-' (case-insensitive), defaultValue in all other cases.

    Examples:
  • toBoolean("YeS", false) returns true;
  • toBoolean("false", false) returns false;
  • toBoolean(null, true) returns true.