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.

001.package com.etretatlogiciels.samples.string.format;
002. 
003.import static java.lang.System.out;
004. 
005.import java.util.Date;
006. 
007.public class StringFormat
008.{
009.   public static void simpleFormat()
010.   {
011.       out.println( "simpleFormat():" );
012.       out.println( String.format( "    Hi %s, you owe me $%5.2f.", "Jack", 25. ) );
013.   }
014. 
015.   public static void testArgumentIndex()
016.   {
017.       out.println( "\ntestArgumentIndex():" );
018.       out.println( String.format( "    A number may be formatted as a string \"%1$s\""
019.                     + " or a number %1$d", 10 ) );
020. 
021.       // note the %n format specifier. It starts a new line
022.       // but does not consume an argument index from the list
023.       out.println( String.format( "%n    Mixing indexed and unindexed arguments: "
024.             + "%n    %5$s %s %2$s %s %4$s %s %s", "one", "two", "three", "four", "five" ) );
025.   }
026. 
027.   public static void testCharacter()
028.   {
029.       out.println( "\ntestCharacter():" );
030.       out.println( String.format( "    '%1$s', '%1$c', '%1$C'", 97 ) );
031.   }
032. 
033.   public static void testInteger()
034.   {
035.       out.println( "\ntestInteger():" );
036.       out.println( String.format( "    %d, %o, %h, %H", 161, 161, 161, 161 ) );
037. 
038.       // try big number with and without group separator
039.       out.println( String.format( "    %1$d, %1$,d", 161161161161L ) );
040.   }
041. 
042.   public static void testFloat()
043.   {
044.       out.println( "\ntestFloat():" );
045.       out.println( String.format( "    %1$.2e, %1$.2f, %1$.2g, %1$.2a", 12345678.9999932 ) );
046.   }
047. 
048.   public static void testDate()
049.   {
050.       out.println( "\ntestDate():" );
051. 
052.       long currentTime = System.currentTimeMillis();
053.       Date date        = new java.util.Date( currentTime );
054. 
055.       out.println( String.format( "    Current Time: %1$tm/%1$td/%1$tY %1$tH:%1$tM:%1$tS",
056.                          currentTime ) );
057. 
058.       // same as above but using shorthand notation
059.       out.println( String.format( "    Current Time (using composition suffix): %1$tD %1$tT",
060.                          currentTime ) );
061. 
062.       out.println( String.format( "    Current Time (using Date object): %1$tm/%1$td/%1$tY %1$tH:%1$tM:%1$tS",
063.                          date ) );
064.   }
065. 
066.   public static void testFlags()
067.   {
068.       out.println( "\ntestFlags():" );
069.       out.println( String.format( "    '%1$s', '%1$#s', '%1$-10.8s', '%1$.12s', '%1$-25s'", "Huge Fruit, Inc." ) );
070.   }
071. 
072.   public static void testGeneral()
073.   {
074.       out.println( "\ntestGeneral():" );
075.       out.println(
076.       // any object can be formatted as string
077.       // upper case S converts string to upper case
078.             String.format( "    %s, %S, %S, %S, %s", "String", "String", null, ( byte ) 1, 5.6 ) );
079. 
080.       out.println(
081.       // any object can be formatted as boolean
082.       // upper case B prints TRUE or FALSE
083.             String.format( "    %b, %B, %b, %B", "String", null, ( byte ) 1, 5.6 ) );
084. 
085.       out.println(
086.       // any object can be formatted as hex
087.       // upper case H prints hex in uppercase
088.             String.format( "    %h, %H, %H, %h, %H", "161", null, 161, new Integer( 161 ), 5.6 ) );
089. 
090.       // What's the effect of width.precision on String?
091.       out.println( String.format( "    \"%1$s\", \"%1$14s\", \"%1$14.2s\"", "Hello" ) );
092.   }
093. 
094.   public static void main( String[] args )
095.   {
096.       simpleFormat();
097.       testArgumentIndex();
098.       testCharacter();
099.       testInteger();
100.       testFloat();
101.       testDate();
102.       testFlags();
103.       testGeneral();
104.   }
105.}

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"