Lesson Wednesday

In this lesson we'll explore a small shortcut that makes concatenating strings and other information together much, much simpler and more sophisticated

Concatenation with +

So far, we have been combining string variables using concatenation with the + operator, like this:

String myName = "Jamie";
String sentenceOne = "Hi, my name is " + myName + ". Nice to meet you.";

java.lang.String sentenceOne = "Hi, my name is Jamie. Nice to meet you."

Formatted Strings

Another way to do this is to use Java's formatted string. If we rewrote the exact same code above using formatted strings, it would look like this:

String myName = "Jamie";
String sentenceOne = String.format("Hi, my name is %s. Nice to meet you", myName);

java.lang.String sentenceOne = "Hi, my name is Jamie. Nice to meet you"

.format() Method

As you can see, the .format() method takes multiple arguments. The first argument is called the format string. It contains a standard string, but the symbol %s is included as a sort of placeholder. This symbol is sometimes referred to as a format specifier.

Different Symbol Formats

String

As we saw in the example above, we use the %s format specifier to concatenate a string into a formatted string:

> String sentenceWithString = String.format("Hi, my name is %s. Nice to meet you", "Jamie");

java.lang.String sentenceWithString = "Hi, my name is Jamie. Nice to meet you"

Integer

If you want to use an integer you would use %d as your format specifier:

> String sentenceWithInteger = String.format("I brush my teeth %d times a day", 2);

java.lang.String sentenceWithInteger = "I brush my teeth 2 times a day"

Character

Likewise, %c represents a single character. You could use it as your symbol to concatenate a char into a String like this:

> String sentenceWithChar = String.format("My name begins with the letter %c", 'e');

java.lang.String sentenceWithChar = "My name begins with the letter e"

Remember, char types must be surrounded with single quotations!

Floating Point Numbers

We could concatenate a floating point number (ie: Essentially a number with a decimal place in it) with %f like this:

> String sentenceWithFloat = String.format("The value of pi is %f", 3.14);

java.lang.String sentenceWithFloat = "The value of pi is 3.140000"

Concatenating Multiple Values

You can add use multiple format specifiers in a single formatted string. For instance:

> String myName = "Jamie";
> String otherName = "Cameron";
> String sentenceWithTwoStrings = String.format("Hello, %s. Nice to meet you, too. My name is %s", myName, otherName);

java.lang.String sentenceWithTwoStrings = "Hello, Jamie. Nice to meet you, too. My name is Cameron"

As you can see, when you use multiple format specifiers in a formatted string, the provided arguments are evaluated from left to right. In the example above, the first %s is replaced by the first argument after the formatted string, myName. Then, the second %s is replaced with the next argument provided: otherName.

You can even use format specifiers of different types:

> String sentenceWithStringAndInteger = String.format("Hello, %s. Nice to meet you, too. Did you know I brush my teeth %d times a day?", "Test Name", 2);

java.lang.String sentenceWithStringAndInteger = "Hello, Test Name. Nice to meet you, too. Did you know I brush my teeth 2 times a day?"

Try out formatted strings instead of the + operator to concatenate values in future projects!

Terminology


  • Formatted String: A string containing a format specifier (see below) to concatenate additional values into it using the .format() method.

  • Format Specifier: The symbol used in a formatted string. Such as %s to represent other strings, %f to represent floats, or %d to represent integers.

Examples


Floating Point Numbers:

String sentenceWithFloat = String.format("The value of pi is %f", 3.14); 

Chars:

String sentenceWithChar = String.format("My name begins with the letter %c", 'e');

Integers:

String sentenceWithInteger = String.format("I brush my teeth %d times a day", 2);

Additional Resources


  • For more format specifiers, and additional examples check out this cheat sheet.