-1

As title says, I want to know is there any convent way to format string using array in Java.

Let me hold an kotlin example.

var sentences = arrayOf("hello", "world")
var template = "Dialogue: ${sentences[0]}\n" +
                "Dialogue: ${sentences[1]}\n"

template.format(sentences)

print(template)

Above code works well. So how about Java ?

EDIT

I am sorry I have not described my question clearly. And when I run into my real case I found my code cannot work for me now.

In fact, template is read from a file.

var sentences = arrayOf("hello", "world")
var template = File("template.txt").readText()

template.format(sentences)
print(template)

And template.txt file contains:

Dialogue: ${sentences[0]}
Dialogue: ${sentences[1]}

As you see, I want to read file then format the result.

In addition, I am so sorry about this question. Because it seems to be changed to how to format string which is read from file.

CoXier
  • 2,523
  • 8
  • 33
  • 60

3 Answers3

4

The code above is completely wrong use of String.format. Let me explain why.

In the first two line:

var sentences = arrayOf("hello", "world")
var template = "Dialogue: ${sentences[0]}\n" +
              "Dialogue: ${sentences[1]}\n"

Here, template is already there, it's a String, its value is "Dialogue: hello\nDialogue: world", which is the result you see.

If you don't trust me, try replace sentences with a non-exist variable name. You'll get a compile error, because the string is connected exactly when you create it. "String template" is just a syntax sugar of +.

Then, you've invoked template.format, which is actually String.format, the returned string is not used.

Since this is already wrong in Kotlin, you can do the same thing in Java easily:

String[] sentences = new String { "hello", "world" };
String template = "Dialogue: " + sentences[0] + "\n" +
                  "Dialogue: " + sentences[1] + "\n";

template.format(sentences);

System.out.print(template);

This code equals to the Kotlin code you've given.

I guess you want this:

String template = String.format("Dialogue: %s%nDialogue: %s%n", sentences);
System.out.print(template);
ice1000
  • 6,406
  • 4
  • 39
  • 85
2

You can use String.format():

String[] sentences = { "hello", "world" };
String template = "Dialogue: %s%nDialogue: %s%n";
String result = String.format(template, arr[0], arr[1]);
Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
  • 2
    OPs example with the array is more akin to `MessageFormat` – Phil Apr 27 '18 at 03:33
  • @Phil I was not aware of that class. I'll leave this answer up if it helps anyone though. – Jonathan Lam Apr 27 '18 at 03:35
  • 2
    In my opinion this would be easier to read with straight string concatenation rather than relying on format strings. The compiler will optimize it automatically with a `StringBuilder`. – Rag Apr 27 '18 at 03:35
  • @BrianGordon That's also very interesting. I'm not too familiar with Java and I guess I need to learn about these utility classes (i.e., `MessageFormat` and `StringBuilder` – Jonathan Lam Apr 27 '18 at 03:38
  • The asker is not using `String.format`. He's just using string concatation, and did a call on `String.format` and drops the return value, then prints the concatation result. – ice1000 Apr 27 '18 at 03:44
  • @ice1000 OP simply asks for "any convent way to format string using array in Java." He does not mention that he did not want to use `String.format`. (He does not even use `String.format()` because his example is in Kotlin, presumably). He is simply asking how to do it in Java. – Jonathan Lam Apr 27 '18 at 03:50
  • 1
    He did use it, `template.format` is the same as `String.format`. – ice1000 Apr 27 '18 at 03:51
0

Use %s for substitutions in the template file. Kotlin (Java) will then call the toString method for each argument and replace them with respect to the order of the arguments.

//template.txt
%s magnificent %s!

//Main.kt
val sentences = arrayOf("hello", "world")
val template = File("template.txt").readText()

template.format(sentences) // hello magnificent world!
Muli Yulzary
  • 2,559
  • 3
  • 21
  • 39