sdsu
Class FormatString

java.lang.Object
  |
  +--sdsu.FormatString

public class FormatString
extends java.lang.Object

This string formatting class emulates some of the functionality of the the standard C library sprintf() function. Since Java doesn't allow for a variable number of arguments of anonymous types, the formatting has been split up into several format member functions with different. arguments.

Some example uses of this class:

 sdsu.FormatString.format("'%5d'", 42)      gives '   42'
 sdsu.FormatString.format("'%-10s'", "hi")  gives 'hi        '
 sdsu.FormatString.format("'%s: 0x%08x'", "address", 8592837)
                                            gives 'address: 0831dc5'
 etc.
 
The floating point to ASCII conversion is very incomplete.

Since all the format member functions are static, you will never have to create a FormatString object.

Author:
Andrew Scherpbier (andrew@sdsu.edu)

Method Summary
static java.lang.String format(java.lang.String fmt, char c)
          Produce a formatted character depending on the format pattern.
static java.lang.String format(java.lang.String fmt, double d)
          Produce a formatted floating point number depending on the format pattern.
static java.lang.String format(java.lang.String fmt, long n)
          Produce a formatted integer depending on the format pattern.
static java.lang.String format(java.lang.String fmt, long n1, long n2)
          Produce a string depending on the format pattern.
static java.lang.String format(java.lang.String fmt, long n1, long n2, long n3)
          Produce a string depending on the format pattern.
static java.lang.String format(java.lang.String fmt, long n1, java.lang.String s2)
          Produce a string depending on the format pattern.
static java.lang.String format(java.lang.String fmt, java.lang.String s)
          Produce a formatted string depending on the format pattern.
static java.lang.String format(java.lang.String fmt, java.lang.String s1, long n2)
          Produce a string depending on the format pattern.
static java.lang.String format(java.lang.String fmt, java.lang.String s1, java.lang.String s2)
          Produce a string depending on the format pattern.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

format

public static java.lang.String format(java.lang.String fmt,
                                      long n)
Produce a formatted integer depending on the format pattern. The formatting rules are identical to those of the sprintf() C library function. The only restriction is that only one '%' escape can be used and the command has to be 'd', 'o', 'x', or 'X'.
Parameters:
fmt - the format string
n - the integer number to be formatted
Returns:
a new String with the formatted text.

format

public static java.lang.String format(java.lang.String fmt,
                                      java.lang.String s)
Produce a formatted string depending on the format pattern. The formatting rules are identical to those of the sprintf() C library function. The only restriction is that only one '%' escape can be used and the command has to be 's'.
Parameters:
fmt - the format string
s - the string to be formatted
Returns:
a new String with the formatted text.

format

public static java.lang.String format(java.lang.String fmt,
                                      char c)
Produce a formatted character depending on the format pattern. The formatting rules are identical to those of the sprintf() C library function. The only restriction is that only one '%' escape can be used and the command has to be 'c'.
Parameters:
fmt - the format string
c - the character to be formatted
Returns:
a new String with the formatted text.

format

public static java.lang.String format(java.lang.String fmt,
                                      double d)
Produce a formatted floating point number depending on the format pattern. The formatting rules are identical to those of the sprintf() C library function. The only restriction is that only one '%' escape can be used and the command has to be 'f'.
Parameters:
fmt - the format string
d - the floating point number to be formatted
Returns:
a new String with the formatted text.

format

public static java.lang.String format(java.lang.String fmt,
                                      long n1,
                                      long n2)
Produce a string depending on the format pattern. This version will format two integers. The formatting rules are identical to those of the sprintf() C library function. The only restriction is that only two '%' escapes can be used and the commands have to be 'd'.
Parameters:
fmt - the format string
n1 - the first integer to be formatted
n2 - the second integer to be formatted
Returns:
a new String with the formatted text.

format

public static java.lang.String format(java.lang.String fmt,
                                      java.lang.String s1,
                                      long n2)
Produce a string depending on the format pattern. This version will format a string and an integer. The formatting rules are identical to those of the sprintf() C library function. The only restriction is that only two '%' escapes can be used and the commands have to be 's' and 'd' (in that order).
Parameters:
fmt - the format string
s1 - the string to be formatted
n2 - the integer to be formatted
Returns:
a new String with the formatted text.

format

public static java.lang.String format(java.lang.String fmt,
                                      long n1,
                                      java.lang.String s2)
Produce a string depending on the format pattern. This version will format an integer and a string. The formatting rules are identical to those of the sprintf() C library function. The only restriction is that only two '%' escapes can be used and the commands have to be 'd' and 's' (in that order).
Parameters:
fmt - the format string
n1 - the integer to be formatted
s2 - the string to be formatted
Returns:
a new String with the formatted text.

format

public static java.lang.String format(java.lang.String fmt,
                                      java.lang.String s1,
                                      java.lang.String s2)
Produce a string depending on the format pattern. This version will format two strings. The formatting rules are identical to those of the sprintf() C library function. The only restriction is that only two '%' escapes can be used and the commands have to be '2'.
Parameters:
fmt - the format string
s1 - the first string to be formatted
s2 - the second string to be formatted
Returns:
a new String with the formatted text.

format

public static java.lang.String format(java.lang.String fmt,
                                      long n1,
                                      long n2,
                                      long n3)
Produce a string depending on the format pattern. This version will format three integers. The formatting rules are identical to those of the sprintf() C library function. The only restriction is that only three '%' escapes can be used and the commands have to be 'd'.
Parameters:
fmt - the format string
n1 - the first integer to be formatted
n2 - the second integer to be formatted
n3 - the third integer to be formatted
Returns:
a new String with the formatted text.