public class

Strings

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

Summary

Public Constructors
Strings()
Public Methods
static String[] decodeArray(String s)
Decode an encoded array of strings.
static List<String> decodeList(String s)
Decode an encoded list of strings.
static Map<String, String> decodeMap(String s)
Decode an encoded map of strings to strings.
static String decodeUTF8(byte[] bytes)
Decode a byte buffer using UTF-8.
static String decodeUTF8(byte[] bytes, int offset, int length)
Decode a byte buffer using UTF-8.
static String encodeArray(String[] array)
Encode an array of strings.
static String encodeList(List<String> list)
Encode a list of strings.
static String encodeMap(Map<String, String> map)
Encode a map of strings to strings.
static byte[] encodeUTF8(String s)
Encode a string using UTF-8.
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 generate(char c, int count)
Generate a repeated-character string.
static int getAsciiLength(byte[] data)
Retrieve the length of the potential String is the data byte array.
static boolean hasBlank(CharSequence s)
Determine if a string contains one or more WSP characters.
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 String join(String separator, Object... objects)
Join the string representations of a sequence of objects using the provided separator.
static String join(String separator, Iterable<?> iterator)
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 ltrim(String s)
Left trim: A leading-characters-only version of trim().
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 replaceNewLines(String s, String repl)
Replace newline characters.
static String rtrim(String s)
Right trim: A trailing-characters-only version of trim().
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 caseSensitive, boolean reverseSearch)
Search a substring inside a bigger string.
static String[] splitLines(String s)
Split a text into an array of Lines.
static String toString(Object o)
A safe version of String.toString.
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

Public Constructors

public Strings ()

Public Methods

public static String[] decodeArray (String s)

Decode an encoded array of strings.

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

public static List<String> decodeList (String s)

Decode an encoded list of strings.

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

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

Decode an encoded map of strings to strings.

Parameters
s optional encoded map
Returns
  • the decoded map

public static String decodeUTF8 (byte[] bytes)

Decode a byte buffer using UTF-8. If UTF-8 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 UTF-8. If UTF-8 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 encodeArray (String[] array)

Encode an array of strings.

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

public static String encodeList (List<String> list)

Encode a list of strings.

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

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

Encode a map of strings to strings.

Parameters
map the map
Returns
  • the encoded map as a string

public static byte[] encodeUTF8 (String s)

Encode a string using UTF-8. If UTF-8 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 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 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)

Retrieve the length of the potential String is the data byte array. The String characters allowed are contained between 0x20 and 0x7F.

Parameters
data byte array representing String
Returns
  • the length of the String.

public static boolean hasBlank (CharSequence s)

Determine if a string contains one or more WSP characters.

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 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

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

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 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 ltrim (String s)

Left trim: A leading-characters-only version of trim().

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 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 rtrim (String s)

Right trim: A trailing-characters-only version of trim().

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 caseSensitive, boolean reverseSearch)

Search a substring inside a bigger string.

Parameters
data buffer to be searched (aka, the haystack)
begin where to start in the case of a forward search, where to end in the case of a backward/reverse search
pattern text that is being searched (aka, the needle)
caseSensitive search is case-sensitibe
reverseSearch search is reverse
Returns
  • index where the substring was found, or -1 if not

public static String[] splitLines (String s)

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 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 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