2012-12-06 29 views
-1

從Java教程:如何在java SE7中格式化字符串?

System.out.format("%s: %s" 
      + " has bowed to me!%n", 
      this.name, bower.getName()); 

但在Java SE7有沒有這樣的方法。 我應該使用哪種方法? 謝謝。

+0

java 7中有很多類,你是否打算檢查API?http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html – PermGenError

回答

3

還有String.formatSystem.out.printf

System.out.printf("%s: %s" 
    + " has bowed to me!%n", 
    this.name, bower.getName()); 

String.format返回結果在String; System.out.printf將結果打印到控制檯。

(並且jbx也是正確的:你爲什麼說Java SE 7中沒有這種方法?)。

+0

儘管是一個很好的選擇,System.out.format()在java 7中,我可以在API中看到它: – PermGenError

4

當然有:

的System.out是一個PrintStream類型,其中有format()

如果你不想輸出,但只是將其存儲在一個字符串,String也提供了相同的方法format()