You can always use String.format(....). i.e.,

String string = String.format("A String %s %2d", aStringVar, anIntVar);

I'm not sure if that is attractive enough for you, but it can be quite handy. The syntax is the same as for printf and java.util.Formatter. I've used it much especially if I want to show tabular numeric data.

Answer from Hovercraft Full Of Eels on Stack Overflow
Discussions

How are we able to reassign the value of String in java if Strings are immutable?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
12
4
August 23, 2022
java - Is there a general string substitution function similar to sl4fj? - Stack Overflow
If you are looking for a solution where you can replace a bunch of variables in a String with values, you can use StrSubstitutor. More on stackoverflow.com
🌐 stackoverflow.com
regex - Java Variable Substitution - Stack Overflow
I am trying to find a way of substituting every $# found in a string, where $ is the literal character '$', and the # is a number (with 1 or more digits), and substituting the $# with the value of a string in a array at # position. ... PS: I just rotated back from C# to Java, so I forgot a ... More on stackoverflow.com
🌐 stackoverflow.com
parsing - Elegant way to do variable substitution in a java string - Stack Overflow
Pretty simple question and my brain is frozen today so I can't think of an elegant solution where I know one exists. I have a formula which is passed to me in the form "A+B" I also have a mapping... More on stackoverflow.com
🌐 stackoverflow.com
January 17, 2013
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-2.6 › org › apache › commons › lang › text › StrSubstitutor.html
StrSubstitutor (Commons Lang 2.6 API) - Apache Commons
Replaces all the occurrences of variables within the given source builder with their matching values from the resolver. Only the specified portion of the builder will be processed. The rest of the builder is not processed, but it is not deleted. ... Internal method that substitutes the variables.
🌐
How to do in Java
howtodoinjava.com › home › string › java text block formatting with variables/expressions
Java Text Block Formatting with Variables/Expressions
January 10, 2024 - Java text blocks have significantly simplified the handling of multiline strings. Incorporating variables or expressions within the text blocks, although not as straightforward as in some other languages, can be achieved efficiently using the String.formatted() method.
🌐
Reddit
reddit.com › r/javahelp › how are we able to reassign the value of string in java if strings are immutable?
r/javahelp on Reddit: How are we able to reassign the value of String in java if Strings are immutable?
August 23, 2022 -

String s= "Hello";

s="Hi";

It prints Hi , How that is possible ,I am confused here . Strings are immutable means we cannot change alter the value stored in it . if we change the value then , a new string object will be created with a new memory location

String b=s.replace("Hello","Hey"); This method replaces the string value and stores in new memory location by creating new string object called b .How reassigning of strings are possible ? if Strings are immutable

🌐
Clapper
software.clapper.org › javautil › api › org › clapper › util › text › VariableSubstituter.html
VariableSubstituter - Software at clapper.org
February 12, 2018 - It copies literal text from the source string to the result string; when it encounters a variable reference, it resolves the variable's value through a separate VariableDereferencer object, and substitutes the resulting value in place of the variable reference in the result string.
🌐
Eclipse
help.eclipse.org › latest › topic › org.eclipse.jdt.doc.user › reference › preferences › run-debug › ref-string_substitution.htm
String Substitution Preferences
Java Development User Guide > Reference > Preferences > Run/Debug · The following preferences can be set using the Run/Debug > String Substitution preference page. This preference page is used solely for the purpose of creating string variables to be reused in other locations that accept string ...
Find elsewhere
🌐
Simplesolution
simplesolution.dev › java-substitute-a-string-in-java-by-replace-variables-map-to-template-string
Substitute a String in Java by replace variables map to Template String using Apache Commons Text
June 13, 2018 - The StringSubstitutor class support define default value for your variable in syntax ${variableName:-defaultValue} with default value define after :- import org.apache.commons.text.StringSubstitutor; import java.util.HashMap; import java.util.Map; public class StringSubstitutorExample2 { public static void main(String[] args) { // Template String String templateString = "Hello ${username}, you have ${count:-0} messages."; // Prepare value map of variables to replace to template String // This value map does not provide "count" key value pair Map<String, String> valuesMap = new HashMap<>(); valuesMap.put("username", "Ben"); // Initialize StringSubstitutor instance with value map StringSubstitutor stringSubstitutor = new StringSubstitutor(valuesMap); // replace value map to template string String result = stringSubstitutor.replace(templateString); System.out.println(result); } } Output:
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › replace
Java String replace() - Replace Substrings | Vultr Docs
December 18, 2024 - The replace() function in Java provides an efficient and straightforward way to manipulate strings by substituting parts of the text with new characters or substrings. Mastering this function enables you to handle everyday coding tasks involving ...
🌐
Apache Commons
commons.apache.org › proper › commons-text › javadocs › api-release › org › apache › commons › text › StringSubstitutor.html
StringSubstitutor (Apache Commons Text 1.9 API)
Returns the flag controlling whether escapes are preserved during substitution. ... Replaces all the occurrences of variables with their matching values from the resolver using the given source array as a template. The array is not altered by this method. ... Replaces all the occurrences of ...
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › use-string-format-java-string-output
Java String formatting with the String.format method (like ‘sprintf’) | alvinalexander.com
If you’re familiar with the sprintf function from other languages like C, Perl, or Ruby, this syntax will look familiar. But, if you’re not familiar with this syntax, what happens in the line of code above is that the %d in my Java String is replaced by the value of the variable RENAME_SUCCEEDED, which in this case happens to be a constant in my class.
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-3.1 › org › apache › commons › lang3 › text › StrSubstitutor.html
StrSubstitutor (Commons Lang 3.1 API) - Apache Commons
Replaces all the occurrences of variables within the given source builder with their matching values from the resolver. Only the specified portion of the builder will be processed. The rest of the builder is not processed, but it is not deleted. ... Internal method that substitutes the variables.
🌐
Baeldung
baeldung.com › home › java › java string › string interpolation in java
String Interpolation in Java | Baeldung
July 23, 2025 - As we can see in the previous example, with this operator, the resulting String contains the values of the variables “interpolate” with other string values. Since it may be adjusted to fit specific needs, this string concatenation method is among the most straightforward and valuable. When using the operator, we don’t need to put the text in quotations. Another approach is using the format() method from the String class.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-illustrate-string-interpolation
String Interpolation in Java - GeeksforGeeks
July 23, 2025 - Below is the simplest way to do string interpolation using "+" operator in Java. This operator is used to concatenate strings by placing variables or string values before or after it by replacing the variables with their actual values.
🌐
Scaler
scaler.com › home › topics › java string interpolation
Java String Interpolation - Scaler Topics
December 20, 2022 - String Interpolation replaces the placeholder with the mentioned variable names assigned to strings and hence makes it efficient to write large variable names or text. ... Here we can observe that the "My name is " part of the string is repetitive ...
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-release › org › apache › commons › lang3 › text › StrSubstitutor.html
StrSubstitutor (Apache Commons Lang 3.11 API)
Replaces all the occurrences of variables within the given source builder with their matching values from the resolver. Only the specified portion of the builder will be processed. The rest of the builder is not processed, but it is not deleted. ... Deprecated.