public class

Strings

extends Object
java.lang.Object
   ↳ com.pnfsoftware.jeb.util.format.Strings

Class Overview

Utility methods for Strings and CharSequences.

Summary

Fields
public static final String LINESEP Line-separator for *this* platform.
Public Constructors
Strings()
Public Methods
static String camelCaseToString(String s, boolean breakOnDigits, boolean keepUppercaseAcronyms)
Convert a camel-case string to a sentence.
static String camelCaseToString(String s)
Convert a camel-case string to a sentence.
static boolean contains(String s, String... elts)
A many-element variant of String.contains.
static int countNonBlankCharacters(CharSequence s)
Count the number of non blank characters in the provided string.
static String decodeASCII(byte[] bytes)
Decode a byte buffer using an ASCII decoder.
static String decodeASCII(byte[] bytes, int offset, int length)
Decode a byte buffer using an ASCII decoder.
static String[] decodeArray(String s)
Decode an encoded array of objects.
static List<String> decodeList(String s)
Decode an encoded list of objects.
static String decodeLocal(byte[] bytes, int offset, int length)
Decode a byte buffer using the local platform's default charset.
static String decodeLocal(byte[] bytes)
Decode a byte buffer using the local platform's default charset.
static Map<String, String> decodeMap(String s)
Decode an encoded map.
static String decodeUTF8(byte[] bytes)
Decode a byte buffer using a UTF-8 decoder.
static String decodeUTF8(byte[] bytes, int offset, int length)
Decode a byte buffer using a UTF-8 decoder.
static Charset determinePotentialEncoding(byte[] data, int offset, int size)
Heuristically determine the encoding of a string.
static byte[] encodeASCII(String s)
Encode a string using an ASCII encoder.
static String encodeArray(Object... array)
Encode an array of objects.
static String encodeList(List<?> list)
Encode a list of objects.
static byte[] encodeLocal(String s)
Encode a string using the local platform's default charset.
static String encodeMap(Map<?, ?> map)
Encode a dictionary.
static byte[] encodeUTF8(String s)
Encode a string using a UTF-8 encoder.
static boolean endsWith(String s, String... elts)
A many-element variant of String.endsWith.
static boolean equals(String a, String b)
A safer version of equals(Object).
static boolean equalsIgnoreCase(String a, String b)
A safer version of equalsIgnoreCase(String)
static String f(String format, Object... args)
Format using the standard US locale.
static String firstLine(String s)
static String generate(char c, int count)
Generate a repeated-character string.
static int getAsciiLength(byte[] data)
static int getAsciiLength(byte[] data, int maxlen)
Retrieve the length of a potentially ASCII-encoded string.
static Comparator<String> getDefaultComparator()
The default JEB String Comparator.
static boolean hasBlank(CharSequence s)
Determine if a string contains one or more WSP characters.
static boolean hasLength(CharSequence s)
Determine if a string is non-null and non-empty.
static boolean hasRtl(CharSequence s)
Determine if a string contains right-to-left (RTL) characters, eg Arabic or Hebrew characters.
static String indentBlock(String blk)
Indent a buffer using a 4-space indentation.
static String indentBlock(String blk, String indent)
Indent a buffer.
static int indexOf(CharSequence text, char c)
Implementation of indexOf for CharSequence.
static int indexOf2(CharSequence text, char c0, char c1)
Find the first one of two characters and return its position.
static boolean isBlank(CharSequence s)
Determine if a character sequence is null, empty, or contains WSP chars exclusively.
static boolean isBlank(String s)
Determine if a string is null, empty, or contains WSP chars exclusively.
static boolean isContainedIn(String s, String... elts)
Determine if a string is contained in an var-arg list of provided strings.
static boolean isNumber(String text)
static boolean isWhitespace(char c)
Determine if a character is a white-space, per the Unicode standard.
static String join(String separator, Iterable<?> iterator)
Join the string representations of a sequence of objects using the provided separator.
static String join(String separator, String[] elts, int begin, int end)
Join a series of non-null strings.
static String join(String separator, Object... objects)
Join the string representations of a sequence of objects using the provided separator.
static String joinList(Iterable<?> objects)
Join the elements of a list using "," as a separator and surround the resulting string with square brackets.
static String joinv(String separator, Object... objects)
Join the string representations of a sequence of objects using the provided separator.
static String joinv(String separator, String defaultValue, Object... objects)
Join the string representations of a sequence of objects using the provided separator.
static String ltrim(String s, char c)
Left trim on a given character.
static String ltrim(String s)
Left trim all chars less than or equal to ' '.
static void makeNewLine(StringBuilder sb)
Append a new-line character to the provided buffer unless the buffer is empty or the last character in the buffer is a new-line.
static CharSequence pad(char c, int iter)
Repeat character c, iter times and build a CharSequence from it.
static String parseUrlParameter(String s, String entry)
Same as parseUrlParameters(String, String...) with a single entry.
static String[] parseUrlParameters(String s, String... entries)
Extract the parameters of a URL-like encoded string.
static String randomUniqueId()
Generate a 32-character long random unique identifier.
static String replaceLast(String str, String target, String replacement)
Replace the last occurrence of target in str by the replacement
static String replaceNewLines(String s, String repl)
Replace newline characters.
static String replaceWhitespaces(String str, char repl)
Efficiently replace all Unicode white-spaces by the provided char.
static String rtrim(String s)
Right trim all chars less than or equal to ' '.
static String rtrim(String s, char c)
Right trim on a given character.
static String safe(Object s)
Get the string representation of the parameter object, or the empty string if the object is null.
static String safe(Object s, String def)
Get the string representation of the parameter object, or the provided string if the object is null.
static String safe2(Object s, String def)
Get the string representation of the parameter object, or the provided non-empty string if the object is null or its string representation is the empty string.
static int search(String data, int begin, String pattern, boolean regex, boolean caseSensitive, boolean reverseSearch)
Search for a sub-string.
static String spaces(int count)
Generate a repeated string of spaces.
static String[] splitLines(String s)
static String[] splitLines(String s, boolean doNotReturnFinalEmptyLine)
Split a text into an array of Lines.
static String[] splitall(String s, String delim)
static boolean startsWith(String s, String... elts)
A many-element variant of String.startsWith.
static String substring(String s, int begin, int end)
Flexible version of substring(int, int).
static String toString(Object o, String defaultValue)
A safe version of String.toString.
static String toString(Object o)
A safe version of String.toString.
static String trim(String s)
Trim (left and right) all chars less than or equal to ' '.
static String trimWhitespaces(String s)
Trim (left and right) all characters considered to be white-space by the Unicode standard.
static String truncate(String s, int maxLength)
Truncate a string.
static String truncateWithSuffix(String s, int maxLength, String suffix)
Truncate a string and append an optional suffix to it if it was actually truncated.
static String urldecodeUTF8(String s)
Decode a URL-encoded string.
static String urlencodeUTF8(String s)
Urlencode a string.
[Expand]
Inherited Methods
From class java.lang.Object

Fields

public static final String LINESEP

Line-separator for *this* platform.

Public Constructors

public Strings ()

Public Methods

public static String camelCaseToString (String s, boolean breakOnDigits, boolean keepUppercaseAcronyms)

Convert a camel-case string to a sentence. Example:

 ThisIsACamelCaseString    -> This is a camel case string
 ThisIsACamel44CaseString  -> This is a camel44 case string
 CountryUSA                -> Country u s a
 
 with breakOnDigits=true:
 ThisIsACamel44CaseString  -> This is a camel 44 case string
 
 with keepUppercaseAcronyms=true:
 CountryUSA                -> Country USA
 
A legal camel-case string always starts with an upper-case letter, and does not contain whitespace characters.

Parameters
s the input camel-case string
breakOnDigits if true, base-10 numbers will also be used as breaks
keepUppercaseAcronyms keep 2+ upper-case letter acronyms intact, eg: CountryUSA would be converted to Country USA instead of Country u s a
Returns
  • the result sentence
Throws
ParseException if the input string was not camel-case formatted

public static String camelCaseToString (String s)

Convert a camel-case string to a sentence. Example:

 ThisIsACamelCaseString -> This is a camel case string
 
A legal camel-case string always starts with an upper-case letter, and does not contain whitespace characters.

Parameters
s the input camel-case string
Returns
  • the result sentence
Throws
ParseException if the input string was not camel-case formatted

public static boolean contains (String s, String... elts)

A many-element variant of String.contains.

Parameters
s the string
elts a list of string elements
Returns
  • true if the string contains at least one of the provided elements

public static int countNonBlankCharacters (CharSequence s)

Count the number of non blank characters in the provided string.

public static String decodeASCII (byte[] bytes)

Decode a byte buffer using an ASCII decoder. If the decoder is not available, the byte buffer is decoded using the system's default decoder.

Parameters
bytes mandatory byte buffer
Returns
  • the decoded string

public static String decodeASCII (byte[] bytes, int offset, int length)

Decode a byte buffer using an ASCII decoder. If the decoder is not available, the byte buffer is decoded using the system's default decoder.

Parameters
bytes byte buffer
offset start offset
length count of bytes to be decoded
Returns
  • the decoded string

public static String[] decodeArray (String s)

Decode an encoded array of objects.

Parameters
s the encoded array
Returns
  • the array of decoded strings

public static List<String> decodeList (String s)

Decode an encoded list of objects.

Parameters
s optional encoded list
Returns
  • the list of decoded strings

public static String decodeLocal (byte[] bytes, int offset, int length)

Decode a byte buffer using the local platform's default charset. This method is potentially dangerous.

Parameters
bytes byte buffer
offset start offset
length count of bytes to be decoded
Returns
  • the decoded string

public static String decodeLocal (byte[] bytes)

Decode a byte buffer using the local platform's default charset. This method is potentially dangerous.

Parameters
bytes mandatory byte buffer
Returns
  • the decoded string

public static Map<String, String> decodeMap (String s)

Decode an encoded map.

Parameters
s optional encoded map
Returns
  • the decoded map

public static String decodeUTF8 (byte[] bytes)

Decode a byte buffer using a UTF-8 decoder. If the decoder is not available, the byte buffer is decoded using the system's default decoder.

Parameters
bytes mandatory byte buffer
Returns
  • the decoded string

public static String decodeUTF8 (byte[] bytes, int offset, int length)

Decode a byte buffer using a UTF-8 decoder. If the decoder is not available, the byte buffer is decoded using the system's default decoder.

Parameters
bytes byte buffer
offset start offset
length count of bytes to be decoded
Returns
  • the decoded string

public static Charset determinePotentialEncoding (byte[] data, int offset, int size)

Heuristically determine the encoding of a string.

Returns
  • null if unknown, else one of ASCII, UTF-8, UTF-16, UTF-16LE, UTF-16BE, UTF-32LE or UTF-32BE

public static byte[] encodeASCII (String s)

Encode a string using an ASCII encoder. If the encoder is not available, the string is encoded using the system's default encoder. This should never happen.

Parameters
s mandatory string
Returns
  • the encoded byte buffer

public static String encodeArray (Object... array)

Encode an array of objects.

Parameters
array the array of objects
Returns
  • the encoded array as a string

public static String encodeList (List<?> list)

Encode a list of objects.

Parameters
list the list of objects
Returns
  • the encoded list as a string

public static byte[] encodeLocal (String s)

Encode a string using the local platform's default charset. This method is potentially dangerous.

Parameters
s mandatory string
Returns
  • the encoded byte buffer

public static String encodeMap (Map<?, ?> map)

Encode a dictionary. The encoding scheme will produce strings like: encodedKey1=encodedValue1&encodedKey2=encodedValue2&...

Parameters
map the map of key/values
Returns
  • the encoded map as a string

public static byte[] encodeUTF8 (String s)

Encode a string using a UTF-8 encoder. If the encoder is not available, the string is encoded using the system's default encoder. This should never happen.

Parameters
s mandatory string
Returns
  • the encoded byte buffer

public static boolean endsWith (String s, String... elts)

A many-element variant of String.endsWith.

Parameters
s the string
elts a list of string elements
Returns
  • true if the string ends with one of the provided elements

public static boolean equals (String a, String b)

A safer version of equals(Object).

Parameters
a first string, may be null
b second string, may be null
Returns
  • true iff both strings are non-null and equals

public static boolean equalsIgnoreCase (String a, String b)

A safer version of equalsIgnoreCase(String)

Parameters
a first string, may be null
b second string, may be null
Returns
  • true iff both strings are non-null and iequals

public static String f (String format, Object... args)

Format using the standard US locale.

public static String firstLine (String s)

public static String generate (char c, int count)

Generate a repeated-character string.

Parameters
c character to repeat
count repeat count (ie, string length)
Returns
  • the string

public static int getAsciiLength (byte[] data)

Parameters
data a bybte array
Returns
  • the length of the string

public static int getAsciiLength (byte[] data, int maxlen)

Retrieve the length of a potentially ASCII-encoded string. The String characters allowed are contained CR, LF, TAB, and any character in the [0x20, 0x7E] range.

Parameters
data a byte array
Returns
  • the length of the string

public static Comparator<String> getDefaultComparator ()

The default JEB String Comparator. It is a combination of NumberComparator and AlphanumCharComparator. See relevant classes for deeper information.

Returns
  • String comparator

public static boolean hasBlank (CharSequence s)

Determine if a string contains one or more WSP characters.

public static boolean hasLength (CharSequence s)

Determine if a string is non-null and non-empty.

Returns
  • the true IFF string contains at least one character

public static boolean hasRtl (CharSequence s)

Determine if a string contains right-to-left (RTL) characters, eg Arabic or Hebrew characters.

public static String indentBlock (String blk)

Indent a buffer using a 4-space indentation.

public static String indentBlock (String blk, String indent)

Indent a buffer.

public static int indexOf (CharSequence text, char c)

Implementation of indexOf for CharSequence. Same behavior as indexOf(int).

Parameters
text string
c char
Returns
  • the index position, or -1 if not found

public static int indexOf2 (CharSequence text, char c0, char c1)

Find the first one of two characters and return its position.

Parameters
text string
c0 first char
c1 second char
Returns
  • the position of the first occurrence of c0 or c1 (whichever came first), -1 if not found

public static boolean isBlank (CharSequence s)

Determine if a character sequence is null, empty, or contains WSP chars exclusively.

Parameters
s the character sequence
Returns
  • true if the sequence is null or blank

public static boolean isBlank (String s)

Determine if a string is null, empty, or contains WSP chars exclusively.

This method is deprecated and left out for compatibility reason only. Use isBlank(CharSequence) instead.

Parameters
s the string
Returns
  • true if the string is null or blank

public static boolean isContainedIn (String s, String... elts)

Determine if a string is contained in an var-arg list of provided strings.

Parameters
s string to be searched
elts the list of elements
Returns
  • true iff the input string was not null and found in the list of elements

public static boolean isNumber (String text)

public static boolean isWhitespace (char c)

Determine if a character is a white-space, per the Unicode standard. This method differs from isWhitespace(char) (Java language definition of a WSP).

public static String join (String separator, Iterable<?> iterator)

Join the string representations of a sequence of objects using the provided separator. Null objects will be formatted as "null".

Parameters
separator a non-null separator
iterator an iterator
Returns
  • the resulting string

public static String join (String separator, String[] elts, int begin, int end)

Join a series of non-null strings.

Parameters
begin inclusive start index
end exclusive end index

public static String join (String separator, Object... objects)

Join the string representations of a sequence of objects using the provided separator. Null objects will be formatted as "null".

This method is error-prone and was deprecated. Use #joinv(String, Object...) joinv instead.

Parameters
separator a non-null separator
objects a sequence of objects
Returns
  • the resulting string

public static String joinList (Iterable<?> objects)

Join the elements of a list using "," as a separator and surround the resulting string with square brackets. Careful, this method does not abide to the common semantic of join.

Parameters
objects a list of objects
Returns
  • the resulting string

public static String joinv (String separator, Object... objects)

Join the string representations of a sequence of objects using the provided separator. Null objects will be formatted as "null".

Parameters
separator a non-null separator
objects an array of objects
Returns
  • the resulting string

public static String joinv (String separator, String defaultValue, Object... objects)

Join the string representations of a sequence of objects using the provided separator.

Parameters
separator a non-null separator
defaultValue String representation for null Objects
objects an array of objects
Returns
  • the resulting string

public static String ltrim (String s, char c)

Left trim on a given character.

public static String ltrim (String s)

Left trim all chars less than or equal to ' '. Note that this method differs from trim() which, for instance, does not consider CR or LF to be WSP.

Parameters
s a string
Returns
  • the left-trimmed string

public static void makeNewLine (StringBuilder sb)

Append a new-line character to the provided buffer unless the buffer is empty or the last character in the buffer is a new-line.

Parameters
sb a string builder

public static CharSequence pad (char c, int iter)

Repeat character c, iter times and build a CharSequence from it. For example pad('0', 4) will return "0000".

Parameters
c inner character
iter times to repeat character.
Returns
  • CharSequence

public static String parseUrlParameter (String s, String entry)

Same as parseUrlParameters(String, String...) with a single entry.

Parameters
s the URL-like string to be parsed, containing a single key-value pair, eg hometype=house
Returns
  • the parameter (without decoding applied)

public static String[] parseUrlParameters (String s, String... entries)

Extract the parameters of a URL-like encoded string. No decoding is taking place. Example:

 - s: "type=home&subtype=house&[another_key]=[another_value]"
 - entries: "type", "subtype"
 - returns: ["home", "house"]
 

Parameters
s the string to be parsed
entries the entries, whose count must match the number of key-value pairs
Returns
  • the list of parameters, as they were (ie, without any decoding applied)

public static String randomUniqueId ()

Generate a 32-character long random unique identifier. The UID returned consists of the digits 0 to 9 and letters a to f (lower-case).

public static String replaceLast (String str, String target, String replacement)

Replace the last occurrence of target in str by the replacement

Parameters
str the string to search in
target the string to search for
replacement the replacement part
Returns
  • the new string with replacement instead of last target occurence or original string if target was not found

public static String replaceNewLines (String s, String repl)

Replace newline characters. This method accepts null strings as input.

Parameters
s a string or null; in the latter case, null will be returned
repl the non-null substitution string

public static String replaceWhitespaces (String str, char repl)

Efficiently replace all Unicode white-spaces by the provided char.

public static String rtrim (String s)

Right trim all chars less than or equal to ' '. Note that this method differs from trim() which, for instance, does not consider CR or LF to be WSP.

Parameters
s a string
Returns
  • the right-trimmed string

public static String rtrim (String s, char c)

Right trim on a given character.

public static String safe (Object s)

Get the string representation of the parameter object, or the empty string if the object is null.

Parameters
s an object, possibly null
Returns
  • the object toString() representation, or the empty string

public static String safe (Object s, String def)

Get the string representation of the parameter object, or the provided string if the object is null.

Parameters
s an object, possibly null
def a non-null string
Returns
  • a non-null string, possibly empty

public static String safe2 (Object s, String def)

Get the string representation of the parameter object, or the provided non-empty string if the object is null or its string representation is the empty string.

Parameters
s an object, possibly null
def a non-null, non-empty string
Returns
  • a string guaranteed to be non-empty

public static int search (String data, int begin, String pattern, boolean regex, boolean caseSensitive, boolean reverseSearch)

Search for a sub-string.

Parameters
data buffer to be searched (aka, the haystack)
begin where to start (inclusive) in the case of a forward search: on success the result will be >= begin; where to end (exclusive) in the case of a backward/reverse search: on success, the result will be < begin
pattern text that is being searched (aka, the needle)
regex if true, the pattern will be treated as a regular expression; if the regex is invalid, it will be treated as a regular string and no error will be reported
caseSensitive search is case-sensitive
reverseSearch search is done in reverse
Returns
  • index where the substring was found, or -1 if nothing was found

public static String spaces (int count)

Generate a repeated string of spaces.

public static String[] splitLines (String s)

public static String[] splitLines (String s, boolean doNotReturnFinalEmptyLine)

Split a text into an array of Lines. Empty lines are returned. The final new-line character(s) are trimmed off. Works for all new lines characters (\r, \n) or sequences of characters (\r\n)

Parameters
s mandatory input string
Returns
  • the lines

public static String[] splitall (String s, String delim)

public static boolean startsWith (String s, String... elts)

A many-element variant of String.startsWith.

Parameters
s the string
elts a list of string elements
Returns
  • true if the string starts with one of the provided elements

public static String substring (String s, int begin, int end)

Flexible version of substring(int, int). Allow Python-like negative indexes for convenience.

Parameters
s a string
begin index in the [-s_length, +s_length] range
end index in the [-s_length, +s_length] range
Returns
  • the substring

public static String toString (Object o, String defaultValue)

A safe version of String.toString.

Parameters
o an object, could be null
defaultValue default String representation if o is null
Returns
  • the String representation of the provided object, or the default value

public static String toString (Object o)

A safe version of String.toString.

Parameters
o an object, could be null
Returns
  • the String representation of the provided object, or "null"

public static String trim (String s)

Trim (left and right) all chars less than or equal to ' '. Note that this method differs from trim() which, for instance, does not consider CR or LF to be WSP.

Parameters
s a string
Returns
  • the trimmed string

public static String trimWhitespaces (String s)

Trim (left and right) all characters considered to be white-space by the Unicode standard.

Parameters
s the input string
Returns
  • the trimmed string

public static String truncate (String s, int maxLength)

Truncate a string.

Parameters
s a string
maxLength positive length
Returns
  • the truncated string, which will contain at most `maxLength` characters

public static String truncateWithSuffix (String s, int maxLength, String suffix)

Truncate a string and append an optional suffix to it if it was actually truncated.

Parameters
s a string
maxLength positive length, which must be greater than or equal to the suffix, if one was provided
suffix optional suffix appended to a string that is actualy truncated

public static String urldecodeUTF8 (String s)

Decode a URL-encoded string.

Parameters
s the encoded string
Returns
  • the decoded string

public static String urlencodeUTF8 (String s)

Urlencode a string. The resulting string will have the following characteristics:

  • a-z, A-Z, 0-9 remain the same
  • ., -, *, _ remain the same
  • space is converted to +
  • all other characters are UTF8 encoded using the "%xx" scheme

Parameters
s the string to be encoded
Returns
  • the encoded string