String prepend: Difference between revisions
→{{header|Java}}
Line 970:
=={{header|Java}}==
Java does not have a prepend method.<br />
<syntaxhighlight lang="java">// prepend▼
The most logical way to prepend a string value is with basic concatenation.
String string = "def";
StringBuilder sb = new StringBuilder("world");▼
string = "abc" + string;
sb.insert(0, "Hello, ");▼
</syntaxhighlight>
You could also use the ''String.concat'' method.
String string = "def";
string = "abc".concat(string);
</syntaxhighlight>
You could use the ''StringBuilder'' class which provides an ''insert'' method.
<syntaxhighlight lang="java">
string.append("def");
</syntaxhighlight>
Additionally, you could use the ''String.format'' or ''String.formatted'' methods.
<syntaxhighlight lang="java">
String string = "def";
string = String.format("abc%s", string);
</syntaxhighlight>
<syntaxhighlight lang="java">
String string = "def";
string = "abc%s".formatted(string);
</syntaxhighlight>
All of these will produce the following output.
<pre>
abcdef
</pre>
=={{header|Javascript}}==
|