Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/luiz-otavio/placeholder4j
Easy-to-use placeholder library written in Java focused on performance.
https://github.com/luiz-otavio/placeholder4j
gnu gradle java library placeholder
Last synced: 14 days ago
JSON representation
Easy-to-use placeholder library written in Java focused on performance.
- Host: GitHub
- URL: https://github.com/luiz-otavio/placeholder4j
- Owner: luiz-otavio
- License: gpl-3.0
- Created: 2021-12-26T23:53:13.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-02T23:49:06.000Z (8 months ago)
- Last Synced: 2024-05-03T07:07:03.974Z (8 months ago)
- Topics: gnu, gradle, java, library, placeholder
- Language: Java
- Homepage:
- Size: 139 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
#
Placeholder4j
## 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.
```java
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.
```java
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.
```java
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:
```groovy
repositories {
maven { url 'https://jitpack.io' }
}dependencies {
implementation 'com.github.luiz-otavio:placeholder4j:{PROJECT_VERSION}'
}
```Kotlin DSL:
```kotlin
repositories() {
maven("https://jitpack.io")
}dependencies() {
implementation("com.github.luiz-otavio:placeholder4j:{PROJECT_VERSION}")
}
```Maven:
```xml
jitpack
jitpack
https://jitpack.io
com.github.luiz-otavio
placeholder4j
{PROJECT_VERSION}
```
## License
This project is licensed under the [GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.html).## Contributing
Open an issue or a pull request and let's discuss about it.