Skip to content

Commit 54517c0

Browse files
committed
Add Strings#trimToOptional
1 parent a0ee40c commit 54517c0

File tree

1 file changed

+14
-0
lines changed
  • log4j-api/src/main/java/org/apache/logging/log4j/util

1 file changed

+14
-0
lines changed

log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Iterator;
2222
import java.util.Locale;
2323
import java.util.Objects;
24+
import java.util.Optional;
2425

2526
/**
2627
* <em>Consider this class private.</em>
@@ -288,6 +289,19 @@ public static String trimToNull(final String str) {
288289
return isEmpty(ts) ? null : ts;
289290
}
290291

292+
/**
293+
* Removes control characters from both ends of this String returning {@code Optional.empty()} if the String is
294+
* empty ("") after the trim or if it is {@code null}.
295+
* @param str The String to trim.
296+
* @return An Optional containing the String.
297+
*
298+
* @see #trimToNull(String)
299+
* @since 2.24.0
300+
*/
301+
public static Optional<String> trimToOptional(final String str) {
302+
return Optional.ofNullable(str).map(String::trim).filter(s -> !s.isEmpty());
303+
}
304+
291305
private Strings() {
292306
// empty
293307
}

0 commit comments

Comments
 (0)