Skip to content

Instantly share code, notes, and snippets.

@codethereforam
Last active July 22, 2018 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codethereforam/9e43f2f8f44b0c83a2be475e46e07c9d to your computer and use it in GitHub Desktop.
Save codethereforam/9e43f2f8f44b0c83a2be475e46e07c9d to your computer and use it in GitHub Desktop.
Java优雅且高效地格式化字符串
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