How to Format Java Message

David Mbochi Njonge Feb 02, 2024
  1. Format Message Using Double Quotes Leveraging MessageFormat
  2. Format Message Using Unicode Characters Leveraging MessageFormat
  3. Format Message Using Escape Sequences Leveraging MessageFormat
  4. Format Message by Replacing Characters Leveraging MessageFormat
How to Format Java Message

In this tutorial, we will learn how to format a message using the Java MessageFormat class provided by the Java API. Formatting is the process of inserting characters and strings dynamically to a message and the MessageFormat provides us with this functionality.

The MessageFormat uses object arguments to form a formatted string using a pattern that shows where the object arguments are inserted.

The object arguments are inserted into the pattern based on the position they are passed in the MessageFormat object parameters. We will use the static format() method of MessageFormat to learn how to format a message.

The first parameter passed to the format() method is our pattern followed by object arguments based on string specifiers we want to insert in our string dynamically. When inserting an element into our pattern, we may use certain characters interpreted differently by the MessageFormat, which may lead to a different result than expected.

For example, using a single quote in a string such as you're within a message being formatted using the MessageFormat results in the string youre. A single quote is used to denote a section that will not be formatted, and note the remaining pattern of our message in the following example is not formatted.

import java.text.MessageFormat;

public class FormatString {
  public static void main(String[] args) {
    String name = "John";
    long age = 30;
    String message =
        MessageFormat.format("Hello {0}, you're turning {1} years old today", name, age);
    System.out.println(message);
  }
}

Output:

Hello John, youre turning {1} years old today

In the next sections, we will cover four different ways that we can use to insert a single quote in our message to ensure our pattern is formatted successfully.

Format Message Using Double Quotes Leveraging MessageFormat

To add a single quote in a message being formatted using MessageFormat, we should use two single quotes '' instead of single quote ' in our string as shown below.

import java.text.MessageFormat;

public class FormatString {
  public static void main(String[] args) {
    String name = "John";
    long age = 30;
    String message =
        MessageFormat.format("Hello {0}, you''re turning {1} years old today", name, age);
    System.out.println(message);
  }
}

Output:

Hello John, you're turning 30 years old today

Note that a single quote is inserted after adding double quotes, and the remaining pattern is formatted with the right value.

Format Message Using Unicode Characters Leveraging MessageFormat

Every character has a Unicode representation. Since Java can read the Unicode characters, we can insert a single quote in our string by using a Unicode representation of a single quote.

import java.text.MessageFormat;

public class FormatString {
  public static void main(String[] args) {
    String name = "John";
    long age = 30;
    String message =
        MessageFormat.format("Hello {0}, you\u2019re turning {1} years old today", name, age);
    System.out.println(message);
  }
}

Output:

Hello John, you're turning 30 years old today

The Unicode character \u2019 adds a single quote in our string, and the remaining pattern of our message is formatted with the right value.

Format Message Using Escape Sequences Leveraging MessageFormat

This method is similar to the one using double quotes but uses an escape sequence \, which is a character with a backslash at the front to insert a sequence of characters. We can insert a single quote by inserting two quote escape sequences in our pattern, as shown below.

import java.text.MessageFormat;

public class FormatString {
  public static void main(String[] args) {
    String name = "John";
    long age = 30;
    String message =
        MessageFormat.format("Hello {0}, you\'\'re turning {1} years old today", name, age);
    System.out.println(message);
  }
}

Output:

Hello John, you're turning 30 years old today

Format Message by Replacing Characters Leveraging MessageFormat

Since we are working with strings, we can let our string remain the same and use the replaceAll() method to replace the single quote with a double quote which finally adds a single quote in our string.

import java.text.MessageFormat;

public class FormatString {
  public static void main(String[] args) {
    String name = "John";
    long age = 30;
    String message = MessageFormat.format(
        "Hello {0}, you're turning {1} years old today".replaceAll("'", "''"), name, age);
    System.out.println(message);
  }
}

Output:

Hello John, you're turning 30 years old today
David Mbochi Njonge avatar David Mbochi Njonge avatar

David is a back end developer with a major in computer science. He loves to solve problems using technology, learning new things, and making new friends. David is currently a technical writer who enjoys making hard concepts easier for other developers to understand and his work has been published on multiple sites.

LinkedIn GitHub