public class Strings extends Object
| Modifier and Type | Method and Description |
|---|---|
static <T extends CharSequence> |
hasText(T str)
Check whether the given CharSequence has actual text.
|
static <T extends CharSequence> |
isEmpty(T str)
Check whether the given CharSequence is empty.
|
static <T extends CharSequence> |
requireNonEmpty(T str)
Checks that the specified string is not empty.
|
static <T extends CharSequence> |
requireNonEmpty(T str,
String msg)
Checks that the specified string is not empty and throws a customized
IllegalArgumentException if it is. |
static <T extends CharSequence> |
requireText(T str)
Checks that the specified string contains non-whitespace text.
|
static <T extends CharSequence> |
requireText(T str,
String msg)
Checks that the specified string contains non-whitespace text and throws a
customized
IllegalArgumentException if it does not. |
public static <T extends CharSequence> T requireText(T str)
public Foo(String bar) {
this.bar = Strings.requireText(bar);
}
T - the type of the char sequencestr - the char sequence to check for textstr if it contains non-whitespace textIllegalArgumentException - if str is null or empty or
consists of whitespace text onlypublic static <T extends CharSequence> T requireText(T str, String msg)
IllegalArgumentException if it does not. This method is
designed primarily for doing parameter validation in methods and
constructors, as demonstrated below:
public Foo(String bar) {
this.bar = Strings.requireText(bar, "bar must contain text");
}
T - the type of the char sequencestr - the char sequence to check for textstr if it contains non-whitespace textIllegalArgumentException - if str is null or empty or
consists of whitespace text onlypublic static <T extends CharSequence> boolean hasText(T str)
true if the string is not null, its length
is greater than 0, and it contains at least one non-whitespace character.
Strings.hasText(null) = false
Strings.hasText("") = false
Strings.hasText(" ") = false
Strings.hasText("12345") = true
Strings.hasText(" 12345 ") = true
T - the type of the char sequencestr - the CharSequence to check (may be null)true if the CharSequence is not null, its
length is greater than 0, and it does not contain whitespace onlyCharacter.isWhitespace(char)public static <T extends CharSequence> T requireNonEmpty(T str)
public Foo(String bar) {
this.bar = Strings.requireNonEmpty(bar);
}
T - the type of the char sequencestr - the char sequence to checkstr if it is not emptyIllegalArgumentException - if str is null or emptypublic static <T extends CharSequence> T requireNonEmpty(T str, String msg)
IllegalArgumentException if it is. This method is designed primarily
for doing parameter validation in methods and constructors, as demonstrated
below:
public Foo(String bar) {
this.bar = Strings.requireNonEmpty(bar, "bar must not be empty");
}
T - the type of the char sequencestr - the char sequence to checkstr if it is not emptyIllegalArgumentException - if str is null or emptypublic static <T extends CharSequence> boolean isEmpty(T str)
true if the string is not null and its length is
greater than 0.
Strings.isEmpty(null) = true
Strings.isEmpty("") = true
Strings.isEmpty(" ") = false
Strings.isEmpty("12345") = false
T - the type of the char sequencestr - the CharSequence to check (may be null)true if the CharSequence is not null and
its length is greater than 0Copyright © 2019 mklinger GmbH. All rights reserved.