File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes String.format() cum vsprintf() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String.format() cum vsprintf()" Watch "String.format() cum vsprintf()" New topic
Author

String.format() cum vsprintf()

Russell Bateman
Ranch Hand

Joined: Feb 26, 2008
Posts: 69
In a past life, I could implement a C string-formatting function with multiple, varying (or no) arguments using stdargs.h functionality, no looping and, for example, vsprint(). Please indulge me here: I'm a (very old) C hack trying to mend his ways.

In my new life, I'd like to wrap String.format() that way into a method that accepts multiple, varying (or no) arguments, to wit:



I think the rub is obvious: String.format() is not happy with unmatched conversion specifiers (java.utl.MissingFormatArgumentException). I need a sort of String.vformat() that takes a variable-length array of Objects à la C. My loop here will instead build an Object[]. Or is there magic glue by which I can move objects from the argument list of buildStatement() to String.format()?

As penance in my new life, I try to resist rolling my own in favor of using existing, tried and true solutions out of standard libraries.

Thanks in advance for any help you can give me,

Russ Bateman
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4374
    
    8

I may be missing something obvious here... but String.format already accepts variable arguments. The protocol is:
public static String format(String format, Object... args)

Does that not already do what you want?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Russ,

Welcome to JavaRanch!

I think you may be making life much harder than it needs to be, because String.format() is pretty much interchangeable with vsprintf. Maybe I'm missing something, though, and you're trying to go after some subtlety that's being lost on me.

The varargs passed to buildStatement are all packaged into an array; the parameter "objects" holds that array. You can pass that array directly to String.format():



I am not at all sure what you're trying to do in your original, though, and I see you've never used the "fmt" argument; don't you really just mean:



And of course, given that, why define "buildStatement()" at all, since String.format() already does the right thing?


[Jess in Action][AskingGoodQuestions]
Russell Bateman
Ranch Hand

Joined: Feb 26, 2008
Posts: 69
Oops, I was simplifying code and renamed stmt to fmt in one place. Sorry about the miscue.

Okay, this does turn out to be the solution (and I've already tested it)...



...which I had not envisioned precisely under the influence of how this works in C. My short-sightedness.

Thanks for your quick responses.

Russ
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38487
    
  23
There is an even simpler way to do itA few links: Java™ Tutorials about "formatting" String#format(java.lang.String, java.lang.Object...) method, where there is this "details" link to the Formatter class, where you will find a complete list of the % tags.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String.format() cum vsprintf()