Str Lib gives you fast and easy String formatting via the Str.format()
method:
Str.format("User ID: {}", userId);
We use the same formatting rules as Log4J
/ SLF4J
,
so you should rarely need to mix format styles anymore:
// Example - mixed String format styles can be annoying.
private static final String EXCEPTION_LOG_FMT = "Exception: {}"; // For logging.
private static final String EXCEPTION_STR_FMT = "Exception: %s"; // Then another for String.format() !
Best of all, Str.format()
is benchmarked at over 6x faster than using String.format()
.
- No transitive dependencies
- Tiny lib size - under 6k
- Works with Java 8+
- Apache 2.0 License
<!-- https://mvnrepository.com/artifact/com.terheyden/strlib -->
<dependency>
<groupId>com.terheyden</groupId>
<artifactId>strlib</artifactId>
<version>0.0.3</version>
</dependency>
// https://mvnrepository.com/artifact/com.terheyden/strlib
implementation group: 'com.terheyden', name: 'strlib', version: '0.0.3'
Str.format()
prints null
for null parameters, same as String.format()
, e.g.:
Str.format("User name: {}", null); // "User name: null"
Parsing simply stops when there are no more parameters. For example:
Str.format("{} and {} and {}", "one", "two"); // "one and two and {}"
It's important that you not lose any data, so extra parameters are appended to the end of the String, separated by a space, e.g.:
Str.format("Numbers:", 1, 2, 3); // "Numbers:1 2 3"
Put a backslash in front of it:
Str.format("{} \\{} {}", "red", "blue"); // "red {} blue"
- Cleaned up the explicit vararg signatures; they were confusing