String formatting

String formatting

The static method format is used to return a formatted string using the specified format string and arguments.
The method syntax is as follows,

public static String format(String format, Object… args)
‘format’ – is a String which may contain fixed text and one or more embedded format specifiers.
‘args’ – Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments can vary and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the JVM Specification.

This method is similar to sprintf() function in C except that here it returns the formatted string.

The format specifiers for general, character, and numeric types

The format specifiers for general, character, and numeric types have the following syntax:

%[argument_index$][flags][width][.precision]conversion

Where,

  • The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by “1$”, the second by “2$”, etc.
  • The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion.
  • The optional width is a non-negative decimal integer indicating the minimum number of characters to be written to the output.
  • The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion.
  • The required conversion is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument’s data type.
package com.ibytecode.strings.methods;

import java.util.Date;
public class StringFormat {
	public static void main(String[] args) {
		double d1 = 234.3453;

		String roundedVal = String.format("Value rounded to 2 decimal places %.2f", d1);
		System.out.println(roundedVal);
	}
}

Value rounded to 2 decimal places 234.35

The format specifiers for dates and times

The format specifiers for types which are used to represents dates and times have the following syntax:

%[argument_index$][flags][width]conversion

  • The optional argument_index, flags and width are defined as above.
  • The required conversion is a two character sequence. The first character is ‘t’ or ‘T’. The second character indicates the format to be used.
package com.ibytecode.strings.methods;
import java.util.Date;
public class StringFormat {
	public static void main(String[] args) {
		String date = String.format("%1$tH hours and %1$tM minutes", new Date());
		System.out.println(date);
	}
}

16 hours and 34 minutes

  • The format specifiers which do not correspond to arguments have the following syntax:
  • %[flags][width]conversion

  • The optional flags and width is defined as above.
  • The required conversion is a character indicating content to be inserted in the output.

Example 1:

package com.ibytecode.strings.methods;
import java.util.Date;
public class StringFormat {
	public static void main(String[] args) {
		String s = String.format("%05d", 234);
		System.out.println(s);
	}
}

which prints,
00234

Here we have specified the flag as ‘0’ which means the result will be zero-padded and width as 5.

Example 2:

package com.ibytecode.strings.methods;
import java.util.Date;
public class StringFormat {
	public static void main(String[] args) {
		String s = String.format("%5d", 234);
		System.out.println(s);
	}
}

which prints,
  234

Here we have not specified any flag but specified the width as 5 hence the result will be right justified. To make it left-justified use ‘-‘ as the flag.

String s = String.format(“%-5d”, 234);

The result will be,
234

Conversion

The required conversion is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument’s data type.

For Example,
Conversion character – ‘d’ is used to format the result as a decimal integer and ‘f’ is used to format the result as a decimal number.

Refer the below link for the complete list of conversion characters.
http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.