Skip to content

Easy-to-use placeholder library written in Java focused on performance.

License

Notifications You must be signed in to change notification settings

luiz-otavio/placeholder4j

Repository files navigation

Placeholder4j

License Release Build Issues Forks Stars

Beside of understanding how placeholders are important:

Constantly we need to replace some values in a string. For example, if we have a message like this: Hello {name}, how are you? and we want to replace the {name} with a value, we can use the replaceAll method from the String class.

public static void main(String[]args){
    String message = "Hello {name}, how are you?";
    String name = "John";
    String replacedMessage = message.replaceAll("\\{name\\}", name);
    System.out.println(replacedMessage);
}

But, if we have multiple placeholders, we need to call the replaceAll method multiple times which is not a good practice.

public static void main(String[]args){
    String message = "Hello {name}, how are you? I'm {age} years old.";
    String name = "John";
    String age = "20";
    String replacedMessage = message.replaceAll("\\{name\\}", name).replaceAll("\\{age\\}", age);
    System.out.println(replacedMessage);
}

How Placeholder4j can help you

Placeholder4j is a library that helps you to replace placeholders in a string. It is simple to use, and you can replace multiple placeholders in a single call.

public static void main(String[]args){
    PlaceholderAPI placeholderAPI = new PlaceholderAPI();
    // Registering a new placeholder for the name
    placeholderAPI.register(new VariablePlaceholder("%name%", "John"));
    // Registering a new placeholder for the age
    placeholderAPI.register(new VariablePlaceholder("%age%", "20"));
    
    // Calling PlaceholderAPI to replace it.
    System.out.println(
        placeholderAPI.replace("Hello %name%, how are you? I'm %age% years old.")
    );
}

Calling it, the output will be: Hello John, how are you? I'm 20 years old.

How to install

Groovy DSL:

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.luiz-otavio:placeholder4j:{PROJECT_VERSION}'
}

Kotlin DSL:

repositories() {
    maven("https://jitpack.io")
}

dependencies() {
    implementation("com.github.luiz-otavio:placeholder4j:{PROJECT_VERSION}")
}

Maven:

<repositories>
    <repository>
        <id>jitpack</id>
        <name>jitpack</name>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.github.luiz-otavio</groupId>
        <artifactId>placeholder4j</artifactId>
        <version>{PROJECT_VERSION}</version>
    </dependency>
</dependencies>

License

This project is licensed under the GNU General Public License v3.0.

Contributing

Open an issue or a pull request and let's discuss about it.