Escape Formatter

In this chapter you will learn:

  1. How to use %n and %% specifiers to escape new line and percentage

The %n and %% Specifiers

The %n and %% format specifiers escape sequences. The %n inserts a newline. The %% inserts a percent sign.

You can use the standard escape sequence \n to embed a newline character. Here is an example that demonstrates the %n and %% format specifiers:

import java.util.Formatter;
//from   j a  va  2  s . c o  m
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("line%nline %d%% complete", 88);
    System.out.println(fmt);
  }
}

The output:

The following code combines the %n, %d %% to display file copy progress information.

import java.util.Formatter;
// j ava2  s  . c o m
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("Copying file%nTransfer is %d%% complete", 88);
    System.out.println(fmt);

  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to set the minimum field width for Java Formatter
  2. How to output formatter data for in a table
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words