Last active
July 22, 2018 10:37
-
-
Save codethereforam/9e43f2f8f44b0c83a2be475e46e07c9d to your computer and use it in GitHub Desktop.
Java优雅且高效地格式化字符串
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.helpers.MessageFormatter; | |
/** | |
* @author thinkam | |
* @date 2018/07/22 | |
*/ | |
public class StringFormatterUtil { | |
/** | |
* elegant and efficient way to format string | |
* | |
* <pre> | |
* precondition: import slf4j-api or copy relative code in slf4j-api | |
* example: | |
* code: StringFormatterUtil.format("{} eats both {} and {}", "Tom", "apple", "cherry") | |
* result: "Tom eats both apple and cherry" | |
* </pre> | |
* | |
* @author thinkam | |
* @date 2018/7/22 18:35 | |
* @param format The message pattern which will be parsed and formatted | |
* @param arguments An array of arguments to be substituted in place of formatting anchors | |
* @return formatted string | |
*/ | |
public static String format(String format, Object... arguments) { | |
return MessageFormatter.arrayFormat(format, arguments).getMessage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment