class Strings
extends java.lang.Object
Modifier | Constructor and Description |
---|---|
private |
Strings() |
Modifier and Type | Method and Description |
---|---|
(package private) static java.lang.String |
truncateAtMaxLength(java.lang.String source,
int maxLength,
boolean addEllipsis)
If this given string is of length
maxLength or less, it will
be returned as-is. |
private static int |
unicodePreservingIndex(java.lang.String str,
int index)
Normalizes
index such that it respects Unicode character
boundaries in str . |
private static java.lang.String |
unicodePreservingSubstring(java.lang.String str,
int begin,
int end)
Returns a substring of
str that respects Unicode character
boundaries. |
static java.lang.String truncateAtMaxLength(java.lang.String source, int maxLength, boolean addEllipsis)
maxLength
or less, it will
be returned as-is.
Otherwise, it will be truncated to maxLength
, regardless of whether
there are any space characters in the String. If an ellipsis is requested
to be appended to the truncated String, the String will be truncated so
that the ellipsis will also fit within maxLength.
If no truncation was necessary, no ellipsis will be added.source
- the String to truncate if necessarymaxLength
- the maximum number of characters to keepaddEllipsis
- if true, and if the String had to be truncated,
add "..." to the end of the String before returning. Additionally,
the ellipsis will only be added if maxLength is greater than 3.private static int unicodePreservingIndex(java.lang.String str, int index)
index
such that it respects Unicode character
boundaries in str
.
If index
is the low surrogate of a Unicode character,
the method returns index - 1
. Otherwise, index
is
returned.
In the case in which index
falls in an invalid surrogate pair
(e.g. consecutive low surrogates, consecutive high surrogates), or if
if it is not a valid index into str
, the original value of
index
is returned.
str
- the Stringindex
- the index to be normalizedprivate static java.lang.String unicodePreservingSubstring(java.lang.String str, int begin, int end)
str
that respects Unicode character
boundaries.
The string will never be split between a [high, low] surrogate pair,
as defined by Character.isHighSurrogate(char)
and
Character.isLowSurrogate(char)
.
If begin
or end
are the low surrogate of a Unicode
character, it will be offset by -1.
This behavior guarantees that
str.equals(StringUtil.unicodePreservingSubstring(str, 0, n) +
StringUtil.unicodePreservingSubstring(str, n, str.length()))
is
true for all n
.
This means that unlike String.substring(int, int)
, the length of
the returned substring may not necessarily be equivalent to
end - begin
.
str
- the original Stringbegin
- the beginning index, inclusiveend
- the ending index, exclusivejava.lang.IndexOutOfBoundsException
- if the begin
is negative,
or end
is larger than the length of str
, or
begin
is larger than end