Class StringUtils

java.lang.Object
com.github.tommyettinger.textra.utils.StringUtils

public final class StringUtils extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    Appends the 8-digit unsigned hex format of number to the given StringBuilder.
    appendUnsignedHex(StringBuilder sb, long number)
    Appends the 16-digit unsigned hex format of number to the given StringBuilder.
    static float
    floatFromDec(CharSequence cs, int start, int end)
    Reads in a CharSequence containing only decimal digits (0-9) with an optional sign at the start and an optional decimal point anywhere in the CharSequence, and returns the float they represent, reading until it encounters the end of the sequence or any invalid char, then returning the result if valid, or 0 if nothing could be read.
    static char
    hexChar(int number)
    Converts the given number, which should be between 0 and 15 inclusive, to its corresponding hex digit as a char.
    static char
    hexChar(long number)
    Converts the given number, which should be between 0 and 15 inclusive, to its corresponding hex digit as a char.
    static int
    hexCode(char c)
    An overly-permissive, but fast, way of looking up the numeric value of a hex digit provided as a char.
    static int
    indexAfter(String text, String search, int from)
    Returns the next index just after the end of search starting at from in text.
    static int
    intFromDec(CharSequence cs, int start, int end)
    Reads in a CharSequence containing only decimal digits (only 0-9) with an optional sign at the start and returns the int they represent, reading at most 10 characters (11 if there is a sign) and returning the result if valid, or 0 if nothing could be read.
    static int
    intFromHex(CharSequence cs, int start, int end)
    Reads in a CharSequence containing only hex digits (only 0-9, a-f, and A-F) with an optional sign at the start and returns the int they represent, reading at most 8 characters (9 if there is a sign) and returning the result if valid, or 0 if nothing could be read.
    static boolean
    isLowerCase(char c)
    Returns true if c is a lower-case letter, or false otherwise.
    static boolean
    isUpperCase(char c)
    Returns true if c is an upper-case letter, or false otherwise.
    static String
    join(CharSequence delimiter, CharSequence... items)
     
    static long
    longFromDec(CharSequence cs, int start, int end)
    Reads in a CharSequence containing only decimal digits (only 0-9) with an optional sign at the start and returns the long they represent, reading at most 19 characters (20 if there is a sign) and returning the result if valid, or 0 if nothing could be read.
    static long
    longFromHex(CharSequence cs, int start, int end)
    Reads in a CharSequence containing only hex digits (only 0-9, a-f, and A-F) with an optional sign at the start and returns the long they represent, reading at most 16 characters (17 if there is a sign) and returning the result if valid, or 0 if nothing could be read.
    static String
    safeSubstring(String source, int beginIndex, int endIndex)
    Like String.substring(int, int) but returns "" instead of throwing any sort of Exception.
    static String
    Shuffles the words in text using MathUtils.random, joins them with a space as the delimiter, and returns that String.
    static String
    shuffleWords(String text, Random generator)
    Shuffles the words in text using the given Random generator, joins them with a space as the delimiter, and returns that String.
    static String
    unsignedHex(int number)
    Allocates a new 8-char String filled with the unsigned hex format of number, and returns it.
    static String
    unsignedHex(long number)
    Allocates a new 16-char String filled with the unsigned hex format of number, and returns it.
    static char[]
    unsignedHexArray(int number)
    Allocates a new 8-char array filled with the unsigned hex format of number, and returns it.
    static char[]
    unsignedHexArray(long number)
    Allocates a new 16-char array filled with the unsigned hex format of number, and returns it.

    Methods inherited from class java.lang.Object

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

    • join

      public static String join(CharSequence delimiter, CharSequence... items)
    • shuffleWords

      public static String shuffleWords(String text)
      Shuffles the words in text using MathUtils.random, joins them with a space as the delimiter, and returns that String.
      Parameters:
      text - a String containing typically many whitespace-separated words
      Returns:
      text with its words shuffled randomly
    • shuffleWords

      public static String shuffleWords(String text, Random generator)
      Shuffles the words in text using the given Random generator, joins them with a space as the delimiter, and returns that String. The generator can be seeded to get replicable results.
      Parameters:
      text - a String containing typically many whitespace-separated words
      Returns:
      text with its words shuffled randomly
    • hexCode

      public static int hexCode(char c)
      An overly-permissive, but fast, way of looking up the numeric value of a hex digit provided as a char. This does not use a table lookup. It will return garbage if not given a valid hex digit, but will not crash or throw an Exception on any input. If you know the given digit is between 0 and 9 inclusive, this can also be used to get the numeric value of that digit as decimal, rather than hexadecimal. You could instead use (c & 15) or just (c - '0') in that case, though.
      Parameters:
      c - a char that should really be a valid hex digit (matching the regex [0-9A-Fa-f])
      Returns:
      the numeric value of the given digit char
    • hexChar

      public static char hexChar(int number)
      Converts the given number, which should be between 0 and 15 inclusive, to its corresponding hex digit as a char. This does not use a table lookup. It will return garbage if not given a number in range, but will not crash or throw an Exception on any input.
      Parameters:
      number - an int that should really be between 0 (inclusive) and 15 (inclusive)
      Returns:
      the hex digit that matches the given number
    • hexChar

      public static char hexChar(long number)
      Converts the given number, which should be between 0 and 15 inclusive, to its corresponding hex digit as a char. This does not use a table lookup. It will return garbage if not given a number in range, but will not crash or throw an Exception on any input.
      This overload only exists to ease conversion to hex digits when given a long input. Its body is the same as the overload that takes an int, hexChar(int).
      Parameters:
      number - an int that should really be between 0 (inclusive) and 15 (inclusive)
      Returns:
      the hex digit that matches the given number
    • appendUnsignedHex

      public static StringBuilder appendUnsignedHex(StringBuilder sb, int number)
      Appends the 8-digit unsigned hex format of number to the given StringBuilder. This always draws from only the digits 0-9 and the capital letters A-F for its hex digits.
      Parameters:
      sb - an existing StringBuilder to append to
      number - any int to write in hex format
      Returns:
      sb, after modification, for chaining.
    • appendUnsignedHex

      public static StringBuilder appendUnsignedHex(StringBuilder sb, long number)
      Appends the 16-digit unsigned hex format of number to the given StringBuilder. This always draws from only the digits 0-9 and the capital letters A-F for its hex digits.
      Parameters:
      sb - an existing StringBuilder to append to
      number - any long to write in hex format
      Returns:
      sb, after modification, for chaining.
    • unsignedHexArray

      public static char[] unsignedHexArray(int number)
      Allocates a new 8-char array filled with the unsigned hex format of number, and returns it.
      Parameters:
      number - any int to write in hex format
      Returns:
      a new char array holding the 8-character unsigned hex representation of number
    • unsignedHexArray

      public static char[] unsignedHexArray(long number)
      Allocates a new 16-char array filled with the unsigned hex format of number, and returns it.
      Parameters:
      number - any long to write in hex format
      Returns:
      a new char array holding the 16-character unsigned hex representation of number
    • unsignedHex

      public static String unsignedHex(int number)
      Allocates a new 8-char String filled with the unsigned hex format of number, and returns it.
      Parameters:
      number - any int to write in hex format
      Returns:
      a new String holding the 8-character unsigned hex representation of number
    • unsignedHex

      public static String unsignedHex(long number)
      Allocates a new 16-char String filled with the unsigned hex format of number, and returns it.
      Parameters:
      number - any long to write in hex format
      Returns:
      a new String holding the 16-character unsigned hex representation of number
    • longFromDec

      public static long longFromDec(CharSequence cs, int start, int end)
      Reads in a CharSequence containing only decimal digits (only 0-9) with an optional sign at the start and returns the long they represent, reading at most 19 characters (20 if there is a sign) and returning the result if valid, or 0 if nothing could be read. The leading sign can be '+' or '-' if present. This can also represent negative numbers as they are printed as unsigned longs. This means "18446744073709551615" would return the long -1 when passed to this, though you could also simply use "-1" . If you use both '-' at the start and have the number as greater than Long.MAX_VALUE, such as with "-18446744073709551615", then both indicate a negative number, but the digits will be processed first (producing -1) and then the whole thing will be multiplied by -1 to flip the sign again (returning 1).
      Should be fairly close to Java 8's Long.parseUnsignedLong method, which is an odd omission from earlier JDKs. This doesn't throw on invalid input, though, instead returning 0 if the first char is not a decimal digit, or stopping the parse process early if a non-0-9 char is read before end is reached. If the parse is stopped early, this behaves as you would expect for a number with fewer digits, and simply doesn't fill the larger places.
      Parameters:
      cs - a CharSequence, such as a String, containing decimal digits with an optional sign
      start - the (inclusive) first character position in cs to read
      end - the (exclusive) last character position in cs to read (this stops after 20 characters if end is too large)
      Returns:
      the long that cs represents
    • intFromDec

      public static int intFromDec(CharSequence cs, int start, int end)
      Reads in a CharSequence containing only decimal digits (only 0-9) with an optional sign at the start and returns the int they represent, reading at most 10 characters (11 if there is a sign) and returning the result if valid, or 0 if nothing could be read. The leading sign can be '+' or '-' if present. This can also represent negative numbers as they are printed as unsigned integers. This means "4294967295" would return the int -1 when passed to this, though you could also simply use "-1" . If you use both '-' at the start and have the number as greater than Integer.MAX_VALUE, such as with "-4294967295", then both indicate a negative number, but the digits will be processed first (producing -1) and then the whole thing will be multiplied by -1 to flip the sign again (returning 1).
      Should be fairly close to Java 8's Integer.parseUnsignedInt method, which is an odd omission from earlier JDKs. This doesn't throw on invalid input, though, instead returning 0 if the first char is not a decimal digit, or stopping the parse process early if a non-0-9 char is read before end is reached. If the parse is stopped early, this behaves as you would expect for a number with fewer digits, and simply doesn't fill the larger places.
      Parameters:
      cs - a CharSequence, such as a String, containing decimal digits with an optional sign
      start - the (inclusive) first character position in cs to read
      end - the (exclusive) last character position in cs to read (this stops after 10 characters if end is too large)
      Returns:
      the int that cs represents
    • intFromHex

      public static int intFromHex(CharSequence cs, int start, int end)
      Reads in a CharSequence containing only hex digits (only 0-9, a-f, and A-F) with an optional sign at the start and returns the int they represent, reading at most 8 characters (9 if there is a sign) and returning the result if valid, or 0 if nothing could be read. The leading sign can be '+' or '-' if present. This can also represent negative numbers as they are printed by such methods as String.format() given %X in the formatting string; that is, if the first char of an 8-char (or longer) CharSequence is a hex digit 8 or higher, then the whole number represents a negative number, using two's complement and so on. This means "FFFFFFFF" would return the int -1 when passed to this, though you could also simply use "-1" . If you use both '-' at the start and have the most significant digit as 8 or higher, such as with "-FFFFFFFF", then both indicate a negative number, but the digits will be processed first (producing -1) and then the whole thing will be multiplied by -1 to flip the sign again (returning 1).
      Should be fairly close to Java 8's Integer.parseUnsignedInt method, which is an odd omission from earlier JDKs. This doesn't throw on invalid input, though, instead returning 0 if the first char is not a hex digit, or stopping the parse process early if a non-hex-digit char is read before end is reached. If the parse is stopped early, this behaves as you would expect for a number with fewer digits, and just doesn't fill the larger places.
      Parameters:
      cs - a CharSequence, such as a String, containing hex digits with an optional sign (no 0x at the start)
      start - the (inclusive) first character position in cs to read
      end - the (exclusive) last character position in cs to read (this stops after 8 characters if end is too large)
      Returns:
      the int that cs represents
    • longFromHex

      public static long longFromHex(CharSequence cs, int start, int end)
      Reads in a CharSequence containing only hex digits (only 0-9, a-f, and A-F) with an optional sign at the start and returns the long they represent, reading at most 16 characters (17 if there is a sign) and returning the result if valid, or 0 if nothing could be read. The leading sign can be '+' or '-' if present. This can also represent negative numbers as they are printed by such methods as String.format() given %X in the formatting string; that is, if the first char of a 16-char (or longer) CharSequence is a hex digit 8 or higher, then the whole number represents a negative number, using two's complement and so on. This means "FFFFFFFFFFFFFFFF" would return the long -1 when passed to this, though you could also simply use "-1" . If you use both '-' at the start and have the most significant digit as 8 or higher, such as with "-FFFFFFFFFFFFFFFF", then both indicate a negative number, but the digits will be processed first (producing -1) and then the whole thing will be multiplied by -1 to flip the sign again (returning 1).
      Should be fairly close to Java 8's Long.parseUnsignedLong method, which is an odd omission from earlier JDKs. This doesn't throw on invalid input, though, instead returning 0 if the first char is not a hex digit, or stopping the parse process early if a non-hex-digit char is read before end is reached. If the parse is stopped early, this behaves as you would expect for a number with fewer digits, and just doesn't fill the larger places.
      Parameters:
      cs - a CharSequence, such as a String, containing hex digits with an optional sign (no 0x at the start)
      start - the (inclusive) first character position in cs to read
      end - the (exclusive) last character position in cs to read (this stops after 8 characters if end is too large)
      Returns:
      the long that cs represents
    • floatFromDec

      public static float floatFromDec(CharSequence cs, int start, int end)
      Reads in a CharSequence containing only decimal digits (0-9) with an optional sign at the start and an optional decimal point anywhere in the CharSequence, and returns the float they represent, reading until it encounters the end of the sequence or any invalid char, then returning the result if valid, or 0 if nothing could be read. The leading sign can be '+' or '-' if present.
      This is somewhat similar to the JDK's Float.parseFloat(String) method, but this also supports CharSequence data instead of just String data, and allows specifying a start and end, but doesn't support scientific notation or hexadecimal float notation. This doesn't throw on invalid input, either, instead returning 0 if the first char is not a decimal digit, or stopping the parse process early if a non-decimal-digit char is read before end is reached. If the parse is stopped early, this behaves as you would expect for a number with fewer digits, and simply doesn't fill the larger places.
      Parameters:
      cs - a CharSequence, such as a String, containing digits 0-9 with an optional sign and decimal point
      start - the (inclusive) first character position in cs to read
      end - the (exclusive) last character position in cs to read (this will stop early if it encounters any invalid char)
      Returns:
      the float that cs represents
    • indexAfter

      public static int indexAfter(String text, String search, int from)
      Returns the next index just after the end of search starting at from in text. If search cannot be found at or after from, this returns text.length().
      This is used heavily by the code that loads FNT files, but isn't used much elsewhere.
      Parameters:
      text - the String to look inside
      search - the String to look for in text
      from - the index to start searching in text
      Returns:
      the index in text that is just after the next occurrence of search, starting at from
    • safeSubstring

      public static String safeSubstring(String source, int beginIndex, int endIndex)
      Like String.substring(int, int) but returns "" instead of throwing any sort of Exception.
      Parameters:
      source - the String to get a substring from
      beginIndex - the first index, inclusive; will be treated as 0 if negative
      endIndex - the index after the last character (exclusive); if negative this will be source.length()
      Returns:
      the substring of source between beginIndex and endIndex, or "" if any parameters are null/invalid
    • isLowerCase

      public static boolean isLowerCase(char c)
      Returns true if c is a lower-case letter, or false otherwise. Similar to Character.isLowerCase(char), but should actually work on GWT.
      Parameters:
      c - a char to check
      Returns:
      true if c is a lower-case letter, or false otherwise.
    • isUpperCase

      public static boolean isUpperCase(char c)
      Returns true if c is an upper-case letter, or false otherwise. Similar to Character.isUpperCase(char), but should actually work on GWT.
      Parameters:
      c - a char to check
      Returns:
      true if c is an upper-case letter, or false otherwise.