Tips on how to format string output

       The operation of formatting strings is basically used in daily development work very frequently. After seeing this knowledge article on the How To Do Java website (" https://howtodoinjava.com/ "), I also spent some time I sorted out the implementations of formatted string output that I have used. These implementations seem to cover all the breadth of my knowledge, and I also share them here to see how much gold my so-called breadth of knowledge can have. For details, see code:

package cn.chendd.tips.examples.howtodoinjava.format;

import cn.hutool.core.util.StrUtil;
import com.sun.javafx.binding.StringFormatter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.PropertyPlaceholderHelper;

import java.text.MessageFormat;
import java.util.Formatter;
import java.util.Properties;

/**
 * 字符串格式化
 *
 * @author chendd
 * @date 2023/4/16 21:03
 */
@RunWith(JUnit4.class)
public class StringFormatTest {

    @Test
    public void string() {
        System.out.println(String.format("hello: %s,昨天是星期 %d", "chendd" , 6));
    }

    @Test
    public void string$() {
        //每个 %占位符 对应一个参数
        System.out.println(String.format("hello: %s,昨天是星期 %d,是的 %s 就是我", "chendd" , 6 , "chendd"));
        //每个%$ 对应一个参数索引
        System.out.println(String.format("hello: %1$s,昨天是星期 %2$d,是的 %1$s 就是我", "chendd" , 6));
    }

    @Deprecated
    @Test
    public void printf$() {
        //这种方法没有返回值,适用于打印输出时,而PrintStream需要 IO 流,个人不推荐
        System.out.printf("hello: %1$s,昨天是星期 %2$d,是的 %1$s 就是我%n", "chendd" , 6);
    }

    @Test
    public void formatter() {
        Formatter format1 = new Formatter().format("hello: %1$s,昨天是星期 %2$d,是的 %1$s 就是我", "chendd", 6);
        System.out.println(format1.out());
        Formatter format2 = new Formatter().format("hello: %s,昨天是星期 %d,是的 %s 就是我", "chendd", 6 , "chendd");
        System.out.println(format2.out());
    }

    @Test
    public void stringFormatter() {
        String format1 = StringFormatter.format("hello: %1$s,昨天是星期 %2$d,是的 %1$s 就是我", "chendd", 6).getValue();
        System.out.println(format1);
        String format2 = StringFormatter.format("hello: %s,昨天是星期 %d,是的 %s 就是我", "chendd", 6, "chendd").getValue();
        System.out.println(format2);
    }

    @Test
    public void logback() {
        Logger logger = LoggerFactory.getLogger(StringFormatTest.class);
        logger.info("hello: {},昨天是星期 {},是的 {} 就是我", "chendd" , 6 , "chendd");
    }

    @Test
    public void hutool() {
        String format = StrUtil.format("hello: {},昨天是星期 {},是的 {} 就是我", "chendd", 6, "chendd");
        System.out.println(format);
    }

    @Test
    public void messageFormat() {
        String format = MessageFormat.format("hello: {0},昨天是星期 {1},是的 {0} 就是我", "chendd", 6);
        System.out.println(format);
    }

    @Test
    public void springFormat() {
        Properties props = new Properties();
        props.put("name" , "chendd");
        props.put("date" , "6");

        String text1 = "hello: {name},昨天是星期 {date}";
        String format1 = new PropertyPlaceholderHelper("{", "}").replacePlaceholders(text1, props);
        System.out.println(format1);

        String text2 = "hello: ${name},昨天是星期 ${date}";
        String format2 = new PropertyPlaceholderHelper("${", "}").replacePlaceholders(text2, props);
        System.out.println(format2);
    }

}

code description

(1) The above code covers the String.format function, PrintStream, Formatter, StringFormatter, logback, hutool, messageFormat, springFormat;

(2) %s represents a placeholder of string type, %d represents a placeholder of number type, and there are other types of placeholders such as date and Boolean;

(3) %s represents a placeholder for a parameter, and %s in different positions will occupy the method parameters corresponding to the number of occurrences;

(4) %1$s means to get the placeholder of the first parameter. The parameter at this position must be of string type, and the same parameter can be quoted multiple times;

(5) Both Formatter and StringFormatter are JDK's built-in formatting implementations, but StringFormatter is in com.sun's reserved package and may be deleted in subsequent JDK versions. It is recommended to use the former Formatter;

(6) logback is a placeholder implementation of the amount of parameters provided by the log API;

(7) hutool is a well-known domestic toolkit that provides multiple formatting implementations, such as StrUtil in this article;

(8) MessageFormat is the implementation of formatted text, does not distinguish between parameter types, and can use the method parameter index to occupy the output;

(9) PropertyPlaceholderHelper is a parameter placeholder implementation provided by Spring, which can customize the prefix and suffix of the placeholder;

See the personal blog: Tips on how to format string output

Tips for formatted string output Welcome to Chen Dongdong's personal experience sharing platform https://www.chendd.cn/blog/article/1647607369063784450.html

Guess you like

Origin blog.csdn.net/haiyangyiba/article/details/130189892