Search code examples
javaandroidstringequivalentequivalence

String.Format (.NET) equivalent in Java?


The String.Format in .NET (maybe just VB.NET) convert {0}, {1}, ... into determined String, for example:

Dim St As String = "Test: {0}, {1}"
Console.WriteLine(String.Format(St, "Text1", "Text2"))

I've tried to search in both Google and StackOverflows, but they all return number-string format.


Solution

  • The other suggestions are certainly good, but are more in the style of printf and its lineage which are more recent additions to Java. The code you posted looks to be inspired by MessageFormat.

    String format = "Test: {0}, {1}"
    System.out.println(MessageFormat.format(format, "Text1", "Text2"))
    

    I'm not really certain about what the 'Return: statement is doing though.