KamielDev
KamielDev

Reputation: 579

Substituting variables in a string without the '+' operator

This question concerns Processing code.

I know that you can substitute a variable in a string by doing the following:

String words = "these words";
text("I want to substitute " + words + "!", textX, textY);

However, I was wondering if there's a way to do it like I am used to doing this in Python:

words = "these words";
print('I want to substitute %s!' % words);

I find this much more readable as the strings aren't as cut up with all types of operators and variables.

Upvotes: 1

Views: 584

Answers (2)

Kayaman
Kayaman

Reputation: 73568

You can use printf in Java. It does the String.format() internally, so you can write pretty much in the same way as Python (or any language with printf-like functionality).

String words = "these words";
System.out.printf("I want to substitute %s!", words);

Upvotes: 1

Andronicus
Andronicus

Reputation: 26066

You can use String.format for that:

String formattedString = String.format("I want to substitute %s!", words);

Upvotes: 4

Related Questions