0

So I am learning a bit of Java and I'm just curios if there is an equivalent to the string format with the $ sign like in C#. For example

string s = $"Date: {DateTime.Now}";
// s = Date: 5/27/2020 8:02:25 AM

Is this possible in Java? I find it more convenient than doing it with indexes.

Snackerino
  • 129
  • 9

2 Answers2

1

You can use Java's String Templates feature. It is described in JEP 430, and it appears in JDK 21 as a preview feature. Here is an example use:

String s = STR."Date: \{new DateTime()}";

A string template can interpolate arbitrary code, which may contain newlines and comments that won't show up in the final string:

String time = STR."The time is \{
   // This does custom formatting.
   DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalTime.now())
} right now";

Java's string templates are more versatile, and much safer, than features in other languagues such as C#'s string interpolation and Python's f-strings. For example, string concatenation or interpolation makes SQL injection attacks possible:

String query = "SELECT * FROM Person p WHERE p.last_name = '" + name + "'";
ResultSet rs = conn.createStatement().executeQuery(query);

but this variant (from JEP 430) prevents SQL injection:

PreparedStatement ps = DB."SELECT * FROM Person p WHERE p.last_name = \{name}";
ResultSet rs = ps.executeQuery();
mernst
  • 7,437
  • 30
  • 45
  • Watching Java begrudgingly implement language features ten years after they become mainstream is somewhat amusing. See: String interpolation, lambdas, dataclasses (`record`), streaming API, etc. – Silvio Mayolo Aug 04 '23 at 23:25
  • Java's string templates are completely different, and in my view much better, than string interpolation features in other languages. Sometimes it can be best to get a feature right, even if it takes a bit longer than a quick hack. (While I think Java hit a home run with string templates, I agree that some other features could have been introduced sooner.) – mernst Aug 05 '23 at 00:06
  • Oh, I didn't read the proposal before commenting. It looks like they did a good job. That's basically how Scala's always done it (https://docs.scala-lang.org/scala3/book/string-interpolation.html), but it's nice to see that feature in other languages too. – Silvio Mayolo Aug 05 '23 at 00:16
0

Java provides a type-safe way of concatenating Strings.

Here is a demonstration using jshell:

> jshell

jshell> import java.time.*;

jshell> String s = ("Date: " + LocalDateTime.now());
s ==> "Date: 2020-05-27T09:49:10.476140"

jshell>

Here is a working example program that uses Date formatting:

// File name: Demo.java

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo {
   public static void main(String[] args) {
      Format f = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss a");
      String s = ( "Date: " + f.format(new Date()) );
      System.out.println(s);
   }
}

Output:

> javac Demo.java

> java Demo
Date: 27/03/2020 10:03:02 am
Gopinath
  • 4,066
  • 1
  • 14
  • 16
  • 1
    Thanks, I do know how to concat strings but i wanted to see if Java supports the same way of "String interpolation". – Snackerino May 28 '20 at 12:21