In this tutorial, you will learn how to create String templates in Java. A String template is a String created with dynamic variables as part of the String.
For example, a String created using data from the database is a String template. A common use case for String templates is when creating query parameters.
String templates create query parameters on a link. The query parameters contain dynamic data for querying the database for specific data.
Using String concatenation
Copy and paste the following code into the Main.java file.
package com.javawhizz; public class Main { public static void main(String[] args) { String[] items = {"Iphone pro max"}; String theItem = items.length == 1 ? "item" : "items"; String inventoryDesc = "The inventory has " + items.length +" "+ theItem; System.out.println(inventoryDesc); } }
In this example, the motive is to display a message that shows the number of items in an array. Additionally, if there is only one item, the message should be in singular.
Otherwise, the message should be in plural. The ternary operator controls whether the message should be in singular or plural.
To use String concatenation, you should use an addition sign to form the message. Since the array length is a number, Java will perform type coercion behind the scenes. As a result, the length will get converted to a String.
Output
The inventory has 1 item
String.format() method
Copy and paste the following code into the Main.java file
package com.javawhizz; public class Main { public static void main(String[] args) { String[] items = {"Iphone pro max"}; String theItem = items.length == 1 ? "item" : "items"; String inventoryDesc = String .format("The inventory has %d %s", items.length, theItem); System.out.println(inventoryDesc); } }
This example is the same as the previous one. The only difference is that it uses the String.format()
static method to create a String template.
The String.format()
method expects the format String as the first parameter. You should pass a list of arguments as the values of the second parameter.
In this case, the String has the format String %d
and %s
. With this in place, the method will format the length and the value of the ternary operator. As a result, the resulting String will have the values of the formatted String variables.
Output
The inventory has 1 item
StringBuilder or StringBuffer
Copy and paste the following code into the Main.java file.
package com.javawhizz; public class Main { public static void main(String[] args) { String[] items = {"Iphone pro max"}; String theItem = items.length == 1 ? "item" : "items"; StringBuilder stringBuilder = new StringBuilder(); stringBuilder .append("The inventory has ") .append(items.length) .append(" ") .append(theItem); System.out.println(stringBuilder); } }
The StringBuilder class has append()
and insert()
method that you can use to achieve this. With the append()
method, you can append each of the dynamic variables at the end of the String.
This will create a String template that has the length of the String and the value of the ternary operator. Since StringBuffer has the same methods, you can use the same approach.
Output
The inventory has 1 item
Using text blocks
package com.javawhizz; public class Main { public static void main(String[] args) { String[] items = {"Iphone pro max"}; String theItem = items.length == 1 ? "item" : "items"; String inventoryDesc = """ The inventory has %s %s """.formatted(items.length, theItem); System.out.println(inventoryDesc); } }
Text blocks are a feature of Java 15 and they help you to work with multi-line strings. In this example, the text block contains format strings to help you format the message.
The String contains two %s
format strings. The first %s
formats the length of the array. Contrary, the second %s
formats the value returned by the ternary operator.
To format the text block, you should call the formatted()
method on it and then pass the list of arguments. The list of arguments in this case are the variables for format strings.
Output
The inventory has 1 item
Conclusion
In this tutorial, you have learned how to create a String template in Java. The approaches covered include using String concatenation, String.format()
, StringBuilder
, StringBuffer
, and Text blocks,
0 Comments