Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A function in Java like string.Format("He is {0}","John") in .NET  RSS feed

 
Nguyen Minh Duc
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want a function in Java like
string.Format("He is {0}",John) in .NET
And the output is "He is John".

Please help me.
Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java 1.5 has a Formatter class. PrintStream also has a printf method.
And the String class also has format method, which would do the same.

The format is a bit different though. Not {0}, but C-like stuff. %s, %d, etc...
[ February 01, 2006: Message edited by: Satou kurinosuke ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See java.text.MessageFormat. In Java 5 (JDK 1.5), your example would be

MessageFormat.format("He is {0}", "John");
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MessageFormat ! Didn't know that one. Thanks
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!