public class StringUtilImpl extends Object implements StringUtil
StringUtil
.getInstance()
Modifier and Type | Field and Description |
---|---|
private static StringUtil |
instance |
private static char[] |
SEPARATORS |
EMPTY_CHAR_ARRAY, EMPTY_STRING_ARRAY, FALSE, LINE_SEPARATOR, LINE_SEPARATOR_CR, LINE_SEPARATOR_CRLF, LINE_SEPARATOR_LF, LINE_SEPARATOR_LFCR, NULL, SYSTEM_PROPERTY_LINE_SEPARATOR, TRUE
Constructor and Description |
---|
StringUtilImpl()
The constructor.
|
Modifier and Type | Method and Description |
---|---|
String |
fromCamlCase(String string,
char separator)
This method converts the given
string from caml-case syntax to lower-case using the given separator
as word-boundary. |
List<String> |
fromSeparatedString(CharSequence separatedString,
String separator,
StringSyntax syntax)
This method is like
StringUtil.fromSeparatedString(CharSequence, String, StringSyntax, Collection, ValueConverter)
but expects elements of the type String that do not need additional custom conversion. |
<E> void |
fromSeparatedString(CharSequence separatedString,
String separator,
StringSyntax syntax,
Collection<E> collection,
ValueConverter<? super String,? super E> converter,
Class<E> type)
This method parses the given
separatedString that contains elements separated with separator and
the given syntax and adds these elements to the given collection . |
<E> void |
fromSeparatedString(CharSequence separatedString,
String separator,
StringSyntax syntax,
Collection<E> collection,
ValueConverter<String,E> converter)
This method parses the given
separatedString that contains elements separated with separator and
the given syntax and adds these elements to the given collection . |
void |
fromSeparatedString(CharSequence separatedString,
String separator,
StringSyntax syntax,
Collection<String> collection)
This method is like
StringUtil.fromSeparatedString(CharSequence, String, StringSyntax, Collection, ValueConverter)
but expects elements of the type String that do not need additional custom conversion. |
static StringUtil |
getInstance()
This method gets the singleton instance of this
StringUtilImpl . |
String |
getLineSeparator()
The platform-specific line-separator string.
|
boolean |
isAllLowerCase(String string)
Determines if all characters of the given
string are in lower case . |
boolean |
isAllUpperCase(String string)
Determines if all characters of the given
string are in upper case . |
boolean |
isEmpty(String string)
This method determines if the given string contains no information.
|
boolean |
isEmpty(String string,
boolean trim)
This method determines if the given string is empty.
|
boolean |
isSubstring(char[] string,
String substring,
int offset)
|
boolean |
isSubstring(String string,
String substring,
int offset)
|
String |
padNumber(long number,
int digits)
This method formats a positive number to a string with at least the given number of digits padding it with leading
zeros.
|
String |
padNumber(long number,
int digits,
int radix)
This method formats a positive number to a string using the given
radix with at least the given number of
digits padding it with leading zeros. |
Boolean |
parseBoolean(String booleanValue)
This method parses a boolean value given as string.
|
void |
replace(char[] string,
char oldChar,
char newChar)
This method replaces all occurrences of
oldChar in the char-array given by string with
newChar . |
String |
replace(String string,
String match,
String replace)
This method replaces all occurrences of the string
match with the string replace in the given
string. |
String |
replaceSuffixWithCase(String string,
int suffixLength,
String newSuffixLowerCase)
This method delegates to
StringUtil.replaceSuffixWithCase(String, int, String, Locale) using Locale.ENGLISH . |
String |
replaceSuffixWithCase(String string,
int suffixLength,
String newSuffixLowerCase,
Locale locale)
This method replaces the last
suffixLength number of characters from string with the lower-case
string newSuffixLowerCase with respect to the original case of the given string . |
String |
toCamlCase(String string)
This method converts the given
string to caml-case syntax using the default separators ' ' ,
'-' , '_' and '.' . |
String |
toCamlCase(String string,
char... separators)
This method converts the given
string to caml-case syntax. |
String |
toSeparatedString(Collection<?> collection,
String separator,
StringSyntax syntax)
This method formats the elements given by
collection to a string where each value is formatted to its
string representation and separated by separator . |
<E> String |
toSeparatedString(Collection<E> collection,
String separator,
StringSyntax syntax,
Formatter<E> formatter)
This method is like
StringUtil.toSeparatedString(Collection, String, StringSyntax) but allows to specify an explicit
Formatter to use instead of Object.toString() . |
<E> void |
toSeparatedString(Collection<E> collection,
String separator,
StringSyntax syntax,
Formatter<E> formatter,
Appendable buffer)
This method is like
StringUtil.toSeparatedString(Collection, String, StringSyntax) but allows to specify an explicit
Formatter to use instead of Object.toString() . |
private static StringUtil instance
private static final char[] SEPARATORS
public static StringUtil getInstance()
StringUtilImpl
. Cdi.GET_INSTANCE
before using.public String getLineSeparator()
StringUtil
StringUtil.LINE_SEPARATOR_LF
,
StringUtil.LINE_SEPARATOR_CRLF
, StringUtil.LINE_SEPARATOR_LFCR
, StringUtil.LINE_SEPARATOR_CR
.getLineSeparator
in interface StringUtil
public Boolean parseBoolean(String booleanValue)
StringUtil
parseBoolean
in interface StringUtil
booleanValue
- is the boolean value as string.true
if the given string equals
to true
, false
if it equals
to false
and null
in any other case.Boolean.valueOf(String)
public void replace(char[] string, char oldChar, char newChar)
StringUtil
oldChar
in the char-array given by string
with
newChar
.replace
in interface StringUtil
string
- is the char-array where the replacement should take place.oldChar
- is the character to be replaced.newChar
- is the replacement for oldChar
.String.replace(char, char)
public String replace(String string, String match, String replace)
StringUtil
match
with the string replace
in the given
string.replace
in interface StringUtil
string
- is the string where to replace.match
- is the string that is searched and replaced.replace
- is the string match
is substituted with.match
replaced by replace
.public boolean isSubstring(String string, String substring, int offset)
StringUtil
string
contains
the given
substring
at the given offset
. string.indexOf(substring, offset) == offsetor
string.substring(offset).beginsWith(substring)
isSubstring
in interface StringUtil
string
- is the string potentially containing substring
.substring
- is the substring that should be contained in string
at the given offset
.offset
- is the offset in string
where to check for substring
.true
if the given string
contains
the given
substring
at the given offset
and false
otherwise.public boolean isSubstring(char[] string, String substring, int offset)
StringUtil
string
contains
the given
substring
at the given offset
. string.indexOf(substring, offset) == offsetor
string.substring(offset).beginsWith(substring)
isSubstring
in interface StringUtil
string
- is the char[] representing the string potentially containing substring
.substring
- is the substring that should be contained in string
at the given offset
.offset
- is the offset in string
where to check for substring
.true
if the given string
contains
the given
substring
at the given offset
and false
otherwise.public String replaceSuffixWithCase(String string, int suffixLength, String newSuffixLowerCase)
StringUtil
StringUtil.replaceSuffixWithCase(String, int, String, Locale)
using Locale.ENGLISH
.replaceSuffixWithCase
in interface StringUtil
string
- is the string to replace.suffixLength
- is the length of the suffix from string
to replace.newSuffixLowerCase
- is the new suffix for the given string
in lower-case
.string
with the last suffixLength
characters cut off and replaced by
newSuffixLowerCase
with respect to the original case of string
.StringUtil.replaceSuffixWithCase(String, int, String, Locale)
public String replaceSuffixWithCase(String string, int suffixLength, String newSuffixLowerCase, Locale locale)
StringUtil
suffixLength
number of characters from string
with the lower-case
string newSuffixLowerCase
with respect to the original case of the given string
. Locale.ENGLISH
:string |
suffixLength |
newSuffixLowerCase |
StringUtil.replaceSuffixWithCase(String, int, String) |
---|---|---|---|
foobar | 3 | foo | foofoo |
FOOBAR | 3 | foo | FOOFOO |
FooBar | 3 | foo | FooFoo |
FooBar | 2 | foo | FooBfoo |
replaceSuffixWithCase
in interface StringUtil
string
- is the string to replace.suffixLength
- is the length of the suffix from string
to replace.newSuffixLowerCase
- is the new suffix for the given string
in lower-case
.locale
- is the locale used for case transformation.string
with the last suffixLength
characters cut off and replaced by
newSuffixLowerCase
with respect to the original case of string
.public boolean isEmpty(String string)
StringUtil
isEmpty
in interface StringUtil
string
- is the string to check.true
if the given string is null
or has a trimmed length of zero, false
otherwise.StringUtil.isEmpty(String, boolean)
public boolean isEmpty(String string, boolean trim)
StringUtil
isEmpty
in interface StringUtil
string
- is the string to check.trim
- if whitespaces should be ignored and a string with a trimmed length of zero is considered as empty.true
if the given string is null
or has a (trimmed) length of zero, false
otherwise.public boolean isAllLowerCase(String string)
StringUtil
string
are in lower case
.
string | isAllLowerCase (string) |
---|---|
UPPER_CASE | false |
lower_case | true |
CamlCase | false |
isAllLowerCase
in interface StringUtil
string
- the String
to check.true
if all characters are in lower case
, false
otherwise.public boolean isAllUpperCase(String string)
StringUtil
string
are in upper case
.
string | isAllUpperCase (string) |
---|---|
UPPER_CASE | true |
lower_case | false |
CamlCase | false |
isAllUpperCase
in interface StringUtil
string
- the String
to check.true
if all characters are in upper case
, false
otherwise.public String padNumber(long number, int digits)
StringUtil
padNumber(5, 3)
will return "005"
padNumber(25, 3)
will return "025"
padNumber(100, 3)
will return "100"
padNumber(1234, 3)
will return "1234"
padNumber
in interface StringUtil
number
- is the positive number to format.digits
- is the (minimum) number of digits required.digits
. If the number is less, leading zeros are
appended.public String padNumber(long number, int digits, int radix)
StringUtil
radix
with at least the given number of
digits padding it with leading zeros. padNumber(31, 3, 16)
will return "01f"
padNumber(5, 6, 2)
will return "000101"
digits
: Radix | byte | short | int | long |
---|---|---|---|---|
2 | 8 | 16 | 32 | 64 |
8 | 3 | 6 | 9 | 12 |
10 | 3 | 5 | 9 | 19 |
16 | 2 | 4 | 8 | 16 |
padNumber
in interface StringUtil
number
- is the positive number to format.digits
- is the (minimum) number of digits required.radix
- is the radix to use.digits
. If the number is less, leading zeros are
appended.public String toCamlCase(String string)
StringUtil
string
to caml-case syntax using the default separators ' '
,
'-'
, '_'
and '.'
.toCamlCase
in interface StringUtil
string
- is the string to convert.string
in caml-case syntax.StringUtil.toCamlCase(String, char[])
public String toCamlCase(String string, char... separators)
StringUtil
string
to caml-case syntax. string
that are in the list given by separators
and
capitalizes the first character of the following word. string | toCamlCase(string) |
---|---|
foo bar |
fooBar |
aAa-bBB-CcC |
aAaBBBCcC |
X--m_._l.. |
XML |
toCamlCase
in interface StringUtil
string
- is the string to convert.separators
- is the list of characters that are treated as word-separators.string
in caml-case syntax.public String fromCamlCase(String string, char separator)
StringUtil
string
from caml-case syntax to lower-case using the given separator
as word-boundary. string | separator | fromCamlCase (string, separator) |
---|---|---|
FooBar | - | foo-bar |
someWordMix | . | some.word.mix |
AbbreviationsLikeXMLshouldNotBeCapitalized | _ | abbreviations_like_xmlshould_not_be_capitalized |
FOO_BAR | * | foo_bar |
fromCamlCase
in interface StringUtil
string
- is the string to convert.separator
- is the character to insert at word-boundaries indicated by a switch from lower- to upper-case.string
in lower-case with the given separator
inserted at word-boundaries.StringUtil.toCamlCase(String, char...)
public String toSeparatedString(Collection<?> collection, String separator, StringSyntax syntax)
StringUtil
collection
to a string where each value is formatted to its
string representation
and separated by separator
. collection | separator | syntax.escape |
syntax.quoteStart |
syntax.quoteEnd |
StringUtil.toSeparatedString(Collection, String, StringSyntax) |
---|---|---|---|---|---|
{"abc;", "a,b,c", Integer.valueOf(123), Character.valueOf('\'') } |
"," |
'\\' |
'\0' |
'\0' |
"abc;,a\\,b\\,c,123,'" |
{"abc;", "a,b,c", Integer.valueOf(123), Character.valueOf('\'') } |
"," |
'\\' |
'\'' |
'\'' |
"'abc;','a,b,c','123','\\''" |
{"abc;", "a,b,c", Integer.valueOf(123), Character.valueOf('\'') } |
"; " |
'\\' |
'[' |
']' |
"[abc;]; [a,b,c]; [123]; [']" |
Collection
s with heterogeneous elements can NOT be converted back from String
.toSeparatedString
in interface StringUtil
collection
- is the Collection
with the elements to format as separated string. May be
empty
.separator
- is the String
used to separate elements. It is appended after each but the last element.
Typically this should be a specific character that may be followed by a whitespace. Common separators are
comma (,) or semicolon (;) often also followed by a whitespace (", "
).syntax
- is the StringSyntax
defining escape
as well as
start
and end
of elements.public <E> String toSeparatedString(Collection<E> collection, String separator, StringSyntax syntax, Formatter<E> formatter)
StringUtil
StringUtil.toSeparatedString(Collection, String, StringSyntax)
but allows to specify an explicit
Formatter
to use instead of Object.toString()
.toSeparatedString
in interface StringUtil
E
- is the generic type of the elements in the collection.collection
- is the Collection
with the elements to format as separated string. May be
empty
.separator
- is the String
used to separate elements. It is appended after each but the last element.
Typically this should be a specific character that may be followed by a whitespace. Common separators are
comma (,) or semicolon (;) often also followed by a whitespace (", "
).syntax
- is the StringSyntax
defining escape
as well as
start
and end
of elements.formatter
- is the Formatter
to use.public <E> void toSeparatedString(Collection<E> collection, String separator, StringSyntax syntax, Formatter<E> formatter, Appendable buffer)
StringUtil
StringUtil.toSeparatedString(Collection, String, StringSyntax)
but allows to specify an explicit
Formatter
to use instead of Object.toString()
.toSeparatedString
in interface StringUtil
E
- is the generic type of the elements in the collection.collection
- is the Collection
with the elements to format as separated string. May be
empty
.separator
- is the String
used to separate elements. It is appended after each but the last element.
Typically this should be a specific character that may be followed by a whitespace. Common separators are
comma (,) or semicolon (;) often also followed by a whitespace (", "
).syntax
- is the StringSyntax
defining escape
as well as
start
and end
of elements.formatter
- is the Formatter
to use.buffer
- is where the separated string is appended
to.public List<String> fromSeparatedString(CharSequence separatedString, String separator, StringSyntax syntax)
StringUtil
StringUtil.fromSeparatedString(CharSequence, String, StringSyntax, Collection, ValueConverter)
but expects elements of the type String
that do not need additional custom conversion.fromSeparatedString
in interface StringUtil
separatedString
- is the separated String
of elements to add to the collection.separator
- is the String
used to separate the individual elements in separatedString
. There
should be no separator
after the last element in separatedString
.syntax
- is the StringSyntax
defining escape
as well as
start
and end
of elements.List
of elements from separatedString
.public void fromSeparatedString(CharSequence separatedString, String separator, StringSyntax syntax, Collection<String> collection)
StringUtil
StringUtil.fromSeparatedString(CharSequence, String, StringSyntax, Collection, ValueConverter)
but expects elements of the type String
that do not need additional custom conversion.fromSeparatedString
in interface StringUtil
separatedString
- is the separated String
of elements to add to the collection.separator
- is the String
used to separate the individual elements in separatedString
. There
should be no separator
after the last element in separatedString
.syntax
- is the StringSyntax
defining escape
as well as
start
and end
of elements.collection
- is where to add the elements to. This should be initially empty.public <E> void fromSeparatedString(CharSequence separatedString, String separator, StringSyntax syntax, Collection<E> collection, ValueConverter<String,E> converter)
StringUtil
separatedString
that contains elements separated with separator
and
the given syntax
and adds
these elements to the given collection
.
StringUtil.toSeparatedString(Collection, String, StringSyntax, Formatter)
.fromSeparatedString
in interface StringUtil
E
- is the generic type of the elements in the collection.separatedString
- is the separated String
of elements to add to the collection.separator
- is the String
used to separate the individual elements in separatedString
. There
should be no separator
after the last element in separatedString
.syntax
- is the StringSyntax
defining escape
as well as
start
and end
of elements.collection
- is where to add the elements to. This should be initially empty.converter
- is used to parse the given elements from String
to their actual type ( <E>).public <E> void fromSeparatedString(CharSequence separatedString, String separator, StringSyntax syntax, Collection<E> collection, ValueConverter<? super String,? super E> converter, Class<E> type)
StringUtil
separatedString
that contains elements separated with separator
and
the given syntax
and adds
these elements to the given collection
.
StringUtil.toSeparatedString(Collection, String, StringSyntax, Formatter)
.fromSeparatedString
in interface StringUtil
E
- is the generic type of the elements in the collection.separatedString
- is the separated String
of elements to add to the collection.separator
- is the String
used to separate the individual elements in separatedString
. There
should be no separator
after the last element in separatedString
.syntax
- is the StringSyntax
defining escape
as well as
start
and end
of elements.collection
- is where to add the elements to. This should be initially empty.converter
- is used to parse the given elements from String
to their actual type ( <E>).
May be the GenericValueConverter
.type
- is the Class
reflecting the elements to add to collection
.Copyright © 2001–2016 mmm-Team. All rights reserved.