Java Equivalent to .NET's String.Format

后端 未结 6 1443
故里飘歌
故里飘歌 2020-12-05 17:09

Is there an equivalent to .NET\'s String.Format in Java?

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 17:20

    The 10 cent answer to this is:

    C#'s

    
    String.Format("{0} -- {1} -- {2}", ob1, ob2, ob3)
    

    is equivalent to Java's

    
    String.format("%1$s -- %2$s -- %3$s", ob1, ob2, ob3)
    

    Note the 1-based index, and the "s" means to convert to string using .toString(). There are many other conversions available and formatting options:

    http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

提交回复
热议问题