Skip to content

Java Str library - faster String formatting and more

License

Notifications You must be signed in to change notification settings

terheyden/strlib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Str Lib - Fast Java String Formatting

Quick Example

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() !

Really Fast

Best of all, Str.format() is benchmarked at over 6x faster than using String.format().

Other Nice Things

  • No transitive dependencies
  • Tiny lib size - under 6k
  • Works with Java 8+
  • Apache 2.0 License

How To Use

Maven

<!-- https://mvnrepository.com/artifact/com.terheyden/strlib -->
<dependency>
    <groupId>com.terheyden</groupId>
    <artifactId>strlib</artifactId>
    <version>0.0.3</version>
</dependency>

Gradle

// https://mvnrepository.com/artifact/com.terheyden/strlib
implementation group: 'com.terheyden', name: 'strlib', version: '0.0.3'

Details

How does Str.format() handle nulls?

Str.format() prints null for null parameters, same as String.format(), e.g.:

Str.format("User name: {}", null); // "User name: null"

How are too few parameters handled?

Parsing simply stops when there are no more parameters. For example:

Str.format("{} and {} and {}", "one", "two"); // "one and two and {}"

How are too many parameters handled?

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"

How do I escape a "{}" in my String?

Put a backslash in front of it:

Str.format("{} \\{} {}", "red", "blue"); // "red {} blue"

What's New

v0.0.3

  • Cleaned up the explicit vararg signatures; they were confusing

About

Java Str library - faster String formatting and more

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published