Home | FAQ | Contact me

String.format() stuff in Java

Here's some rambling on String.format() stuff in Java. Most of this is from elsewhere, http://www.xinotes.org/notes/note/1195/ with additions by me.

package com.etretatlogiciels.samples.string.format;

import static java.lang.System.out;

import java.util.Date;

public class StringFormat
{
   public static void simpleFormat()
   {
       out.println( "simpleFormat():" );
       out.println( String.format( "    Hi %s, you owe me $%5.2f.", "Jack", 25. ) );
   }

   public static void testArgumentIndex()
   {
       out.println( "\ntestArgumentIndex():" );
       out.println( String.format( "    A number may be formatted as a string \"%1$s\""
                     + " or a number %1$d", 10 ) );

       // note the %n format specifier. It starts a new line
       // but does not consume an argument index from the list
       out.println( String.format( "%n    Mixing indexed and unindexed arguments: "
             + "%n    %5$s %s %2$s %s %4$s %s %s", "one", "two", "three", "four", "five" ) );
   }

   public static void testCharacter()
   {
       out.println( "\ntestCharacter():" );
       out.println( String.format( "    '%1$s', '%1$c', '%1$C'", 97 ) );
   }

   public static void testInteger()
   {
       out.println( "\ntestInteger():" );
       out.println( String.format( "    %d, %o, %h, %H", 161, 161, 161, 161 ) );

       // try big number with and without group separator
       out.println( String.format( "    %1$d, %1$,d", 161161161161L ) );
   }

   public static void testFloat()
   {
       out.println( "\ntestFloat():" );
       out.println( String.format( "    %1$.2e, %1$.2f, %1$.2g, %1$.2a", 12345678.9999932 ) );
   }

   public static void testDate()
   {
       out.println( "\ntestDate():" );

       long currentTime = System.currentTimeMillis();
       Date date        = new java.util.Date( currentTime );

       out.println( String.format( "    Current Time: %1$tm/%1$td/%1$tY %1$tH:%1$tM:%1$tS",
                          currentTime ) );

       // same as above but using shorthand notation
       out.println( String.format( "    Current Time (using composition suffix): %1$tD %1$tT",
                          currentTime ) );

       out.println( String.format( "    Current Time (using Date object): %1$tm/%1$td/%1$tY %1$tH:%1$tM:%1$tS",
                          date ) );
   }

   public static void testFlags()
   {
       out.println( "\ntestFlags():" );
       out.println( String.format( "    '%1$s', '%1$#s', '%1$-10.8s', '%1$.12s', '%1$-25s'", "Huge Fruit, Inc." ) );
   }

   public static void testGeneral()
   {
       out.println( "\ntestGeneral():" );
       out.println(
       // any object can be formatted as string
       // upper case S converts string to upper case
             String.format( "    %s, %S, %S, %S, %s", "String", "String", null, ( byte ) 1, 5.6 ) );

       out.println(
       // any object can be formatted as boolean
       // upper case B prints TRUE or FALSE
             String.format( "    %b, %B, %b, %B", "String", null, ( byte ) 1, 5.6 ) );

       out.println(
       // any object can be formatted as hex
       // upper case H prints hex in uppercase
             String.format( "    %h, %H, %H, %h, %H", "161", null, 161, new Integer( 161 ), 5.6 ) );

       // What's the effect of width.precision on String?
       out.println( String.format( "    \"%1$s\", \"%1$14s\", \"%1$14.2s\"", "Hello" ) );
   }

   public static void main( String[] args )
   {
       simpleFormat();
       testArgumentIndex();
       testCharacter();
       testInteger();
       testFloat();
       testDate();
       testFlags();
       testGeneral();
   }
}

Console output of above

    simpleFormat():
        Hi Jack, you owe me $25.00.

    testArgumentIndex():
        A number may be formatted as a string "10" or a number 10

        Mixing indexed and unindexed arguments:
        five one two two four three four

    testCharacter():
        '97', 'a', 'A'

    testInteger():
        161, 241, a1, A1
        161161161161, 161,161,161,161

    testFloat():
        1.23e+07, 12345679.00, 1.2e+07, 0x1.79p23

    testDate():
        Current Time: 09/17/2011 09:03:56
        Current Time (using composition suffix): 09/17/11 09:03:56
        Current Time (using Date object): 09/17/2011 09:03:56

    testFlags():
        'Huge Fruit, Inc.', 'Huge Fruit, Inc.', 'Huge Fru  ', 'Huge Fruit, ', 'Huge Fruit, Inc.         '

    testGeneral():
        String, STRING, NULL, 1, 5.6
        true, FALSE, true, TRUE
        beac, NULL, A1, a1, 26700000
        "Hello", "         Hello", "            He"