Class StringUtils
java.lang.Object
com.github.tommyettinger.textra.utils.StringUtils
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringBuilderappendUnsignedHex(StringBuilder sb, int number) Appends the 8-digit unsigned hex format ofnumberto the given StringBuilder.static StringBuilderappendUnsignedHex(StringBuilder sb, long number) Appends the 16-digit unsigned hex format ofnumberto the given StringBuilder.static BitSetdecompressCategory(regexodus.Category category) Takes the compressed bitset inside a RegExodusCategoryand decompresses it to a JDKBitSet.static floatfloatFromDec(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 charhexChar(int number) Converts the given number, which should be between 0 and 15 inclusive, to its corresponding hex digit as a char.static charhexChar(long number) Converts the given number, which should be between 0 and 15 inclusive, to its corresponding hex digit as a char.static inthexCode(char c) An overly-permissive, but fast, way of looking up the numeric value of a hex digit provided as a char.static intindexAfter(String text, String search, int from) Returns the next index just after the end ofsearchstarting atfromintext.static intintFromDec(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 intintFromHex(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 booleanisLowerCase(char c) Returns true ifcis a lower-case letter, or false otherwise.static booleanisUpperCase(char c) Returns true ifcis an upper-case letter, or false otherwise.static Stringjoin(CharSequence delimiter, CharSequence... items) static longlongFromDec(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 longlongFromHex(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 StringsafeSubstring(String source, int beginIndex, int endIndex) LikeString.substring(int, int)but returns "" instead of throwing any sort of Exception.static StringshuffleWords(String text) Shuffles the words intextusingMathUtils.random, joins them with a space as the delimiter, and returns that String.static StringshuffleWords(String text, Random generator) Shuffles the words intextusing the given Random generator, joins them with a space as the delimiter, and returns that String.static StringunsignedHex(int number) Allocates a new 8-char String filled with the unsigned hex format ofnumber, and returns it.static StringunsignedHex(long number) Allocates a new 16-char String filled with the unsigned hex format ofnumber, and returns it.static char[]unsignedHexArray(int number) Allocates a new 8-char array filled with the unsigned hex format ofnumber, and returns it.static char[]unsignedHexArray(long number) Allocates a new 16-char array filled with the unsigned hex format ofnumber, and returns it.
-
Field Details
-
LETTERS
-
LOWER_CASE_LETTERS
-
UPPER_CASE_LETTERS
-
WORD_CHARS
-
SPACE_CHARS
-
-
Method Details
-
join
-
shuffleWords
Shuffles the words intextusingMathUtils.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
Shuffles the words intextusing 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
Appends the 8-digit unsigned hex format ofnumberto 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 tonumber- any int to write in hex format- Returns:
- sb, after modification, for chaining.
-
appendUnsignedHex
Appends the 16-digit unsigned hex format ofnumberto 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 tonumber- 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 ofnumber, 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 ofnumber, 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
Allocates a new 8-char String filled with the unsigned hex format ofnumber, 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
Allocates a new 16-char String filled with the unsigned hex format ofnumber, 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
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 thanLong.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 signstart- the (inclusive) first character position in cs to readend- 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
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 thanInteger.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 signstart- the (inclusive) first character position in cs to readend- 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
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 readend- 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
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 readend- 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
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'sFloat.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 pointstart- the (inclusive) first character position in cs to readend- 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
Returns the next index just after the end ofsearchstarting atfromintext. Ifsearchcannot be found at or afterfrom, this returnstext.length().
This is used heavily by the code that loads FNT files, but isn't used much elsewhere.- Parameters:
text- the String to look insidesearch- the String to look for in textfrom- 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
LikeString.substring(int, int)but returns "" instead of throwing any sort of Exception.- Parameters:
source- the String to get a substring frombeginIndex- the first index, inclusive; will be treated as 0 if negativeendIndex- 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
-
decompressCategory
Takes the compressed bitset inside a RegExodusCategoryand decompresses it to a JDKBitSet. This may improve lookup time for frequently-checked Categories, sinceBitSet.get(int)is quite fast (it runs in O(1) time), whileCategory.contains(char)is... not as fast (it runs in O(n) time, where n is the RLE-compressed size of the entire bitset). A BitSet can also be modified if needed, whereas a Category cannot.- Parameters:
category- a RegExodus Category, such asCategory.Lufor upper-case letters- Returns:
- a new BitSet storing the same contents as the given Category, but optimized for faster access
-
isLowerCase
public static boolean isLowerCase(char c) Returns true ifcis a lower-case letter, or false otherwise. Similar toCharacter.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 ifcis an upper-case letter, or false otherwise. Similar toCharacter.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.
-