-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.

6
  • stackoverflow.com/a/36498517/283366 Commented Apr 27, 2018 at 3:33
  • 1
    Possible duplicate of String variable interpolation Java Commented Apr 27, 2018 at 3:36
  • Why close my question? Have you read my question? Commented Apr 27, 2018 at 4:12
  • @Phil Can please you read my question again? Commented Apr 27, 2018 at 4:21
  • According to your edit, it becomes an extremely complicated question.. Commented Apr 27, 2018 at 5:29

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Is this an answer? (it's also a bit rude)
I've posted the answer. @JonathanLam
I don't see how this is any different from my answer.
I've given an ideal approach (same as you) and the approach same to his example (you don't have)
You are right. But I will tell you why I should do this.
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]);

7 Comments

OPs example with the array is more akin to MessageFormat
@Phil I was not aware of that class. I'll leave this answer up if it helps anyone though.
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.
@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
He did use it, template.format is the same as String.format.
|
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.