public final class StringUtils extends Object
This work can be done by several libraries. The aim of this class is to be able to change the implementation easily to use another library for example.
For example, we could read which library is available in the classpath and use this library instead of forcing users to include Apache Commons Lang library.
Modifier and Type | Method and Description |
---|---|
static String |
capitalize(String str)
Capitalizes a String changing the first character to title case.
|
static String |
join(Iterable<?> iterable,
String separator)
Joins the elements of the provided Iterable into a single String
containing the provided elements.
|
static String |
join(Object[] array,
String separator)
Joins the elements of the provided array into a single String containing
the provided list of elements.
|
static String |
leftPad(String str,
int size,
char padChar)
Left pad a String with a specified character.
|
public static String join(Object[] array, String separator)
Joins the elements of the provided array into a single String containing the provided list of elements.
No delimiter is added before or after the list. A null separator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.
StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "" StringUtils.join(["a", "b", "c"], "--") = "a--b--c" StringUtils.join(["a", "b", "c"], null) = "abc" StringUtils.join(["a", "b", "c"], "") = "abc" StringUtils.join([null, "", "a"], ',') = ",,a"
array
- the array of values to join together, may be nullseparator
- the separator character to use, null treated as ""public static String join(Iterable<?> iterable, String separator)
Joins the elements of the provided Iterable into a single String containing the provided elements.
No delimiter is added before or after the list. A null separator is the same as an empty String ("").
See the examples here: join(Object[], String)
.
iterable
- the Iterable providing the values to join together, may be
nullseparator
- the separator character to use, null treated as ""public static String leftPad(String str, int size, char padChar)
Left pad a String with a specified character.
Pad to a size of size
.
StringUtils.leftPad(null, *, *) = null StringUtils.leftPad("", 3, 'z') = "zzz" StringUtils.leftPad("bat", 3, 'z') = "bat" StringUtils.leftPad("bat", 5, 'z') = "zzbat" StringUtils.leftPad("bat", 1, 'z') = "bat" StringUtils.leftPad("bat", -1, 'z') = "bat"
str
- the String to pad out, may be nullsize
- the size to pad topadChar
- the character to pad withnull
if null String inputpublic static String capitalize(String str)
Capitalizes a String changing the first character to title case. No other characters are changed.
A null
input String returns null
.
StringUtils.capitalize(null) = null StringUtils.capitalize("") = "" StringUtils.capitalize("cat") = "Cat" StringUtils.capitalize("cAt") = "CAt" StringUtils.capitalize("'cat'") = "'cat'"
str
- the String to capitalize, may be nullnull
if null String inputCopyright © 2021. All rights reserved.