HD

Java 字符串格式替换方法有两种,一种是使用String.format(...),另一种是使用MessageFormat.format(...)

如下:

import java.text.MessageFormat;

public class Test {

    public static void main(String[] args) {
        String strTemp = "11111%s22222%s%%s33333";
        String str = String.format(strTemp, "RRRRR", "QQQQQ");
        System.out.println(str);
        
        String strTemp2 = "1111{0}2222{1}";
        String str2 = MessageFormat.format(strTemp2, "RRRR", "EEEE");
        System.out.println(str2);
    }
    
}

 

输出结果:

11111RRRRR22222QQQQQ%s33333
1111RRRR2222EEEE

 

见第一种方法String.format(...),可使用两个百分号做为转换。

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-10-03
  • 2021-09-06
  • 2021-11-22
  • 2021-11-01
  • 2021-05-29
  • 2021-10-23
  • 2021-10-03
猜你喜欢
  • 2022-02-19
  • 2021-11-01
  • 2021-09-06
  • 2021-11-28
  • 2021-10-03
  • 2021-12-07
相关资源
相似解决方案