• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Definition of "#" in a format string

 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a need to left-pad a java string with a number of spaces that depends on my data. I looked for things to do this and found the following:



So now I'm trying to figure out what the "#" means. I looked up Formattable in the java javadocs, and found (only) a set of examples. The set includes the "#", but it does NOT tell me what the flag does.

I have found various references to it specifying an "alternate format", defined by the class, and there is an ALTERNATE test within the example in the Formattable javadocs, but the example to left-pad the string above is not using that class. Does String have a definition for an "alternate format" in relation to Formattable? I can't even find that String implements Formattable, and there's no "#" character in its javadoc.

I know I could "play around with this" and make a reasonable guess, but it frustrates me that I cannot find a definition. Examples are not sufficient instruction, though they help...

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Formatter javadoc:

If the argument is null, then the result is "null". If the argument implements Formattable, then its formatTo method is invoked. Otherwise, the result is obtained by invoking the argument's toString() method.

If the '#' flag is given and the argument is not a Formattable , then a FormatFlagsConversionMismatchException will be thrown.


This leads me to believe that code will throw an exception.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No exception. It works as advertised. I would like to know why, though...

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ralph Cook wrote:No exception. It works as advertised. I would like to know why, though...



From what I could understand after reading the JavaDocs:

#- The result should use a conversion-dependent alternate form


#- it indicates that the argument/value identified by the given index would be used in a default format specific to the type of argument. Lets see few examples:

If 'o'(Formats the argument as an integer in base eight) is used along with '#'- the output will always begin with the radix indicator '0'
If 'x'(Formats the argument as an integer in base sixteen) is used along with '#'- the output will always begin with the radix indicator "0x".
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except the conversion isn't 'o' or 'x'. It's 's'. What does '#' do for 's'?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Except the conversion isn't 'o' or 'x'. It's 's'. What does '#' do for 's'?



I dont think its doing anything special here. But as rightly pointed- I couldn't really understand their use.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic