aspose file tools*
The moose likes Beginning Java and the fly likes A function in Java like string.Format( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A function in Java like string.Format("He is {0}","John") in .NET" Watch "A function in Java like string.Format("He is {0}","John") in .NET" New topic
Author

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

Nguyen Minh Duc
Greenhorn

Joined: Jan 30, 2006
Posts: 2
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.


I love Java
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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 ]

[My Blog]
All roads lead to JavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

See java.text.MessageFormat. In Java 5 (JDK 1.5), your example would be

MessageFormat.format("He is {0}", "John");


[Jess in Action][AskingGoodQuestions]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

MessageFormat ! Didn't know that one. Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: A function in Java like string.Format("He is {0}","John") in .NET