<p>Printf got added to Java with the 1.5 release but I can't seem to find how to send the output to a string rather than a file (which is what sprintf does in C). Does anyone know how to do this? </p>

[{'answer_id': 47049, 'author': 'Eugene Yokota', 'author_id': 3827, 'author_profile': 'https://Stackoverflow.com/users/3827', 'pm_score': 10, 'selected': True, 'text': '<pre><code>// Store the formatted string in \'result\'\nString result = String.format("%4d", i * j);\n\n// Write the result to standard output\nSystem.out.println( result );\n</code></pre>\n\n<p>See <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)" rel="noreferrer">format</a> and its <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax" rel="noreferrer">syntax</a></p>\n'}, {'answer_id': 198356, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 5, 'selected': False, 'text': '<p>Strings are immutable types. You cannot modify them, only return new string instances.</p>\n\n<p>Because of that, formatting with an instance method makes little sense, as it would have to be called like:</p>\n\n<pre><code>String formatted = "%s: %s".format(key, value);\n</code></pre>\n\n<p>The original Java authors (and .NET authors) decided that a static method made more sense in this situation, as you are not modifying the target, but instead calling a format method and passing in an input string.</p>\n\n<p>Here is an example of why <code>format()</code> would be dumb as an instance method. In .NET (and probably in Java), <code>Replace()</code> is an instance method.</p>\n\n<p>You can do this:</p>\n\n<pre><code> "I Like Wine".Replace("Wine","Beer");\n</code></pre>\n\n<p>However, nothing happens, because strings are immutable. <code>Replace()</code> tries to return a new string, but it is assigned to nothing.</p>\n\n<p>This causes lots of common rookie mistakes like:</p>\n\n<pre><code>inputText.Replace(" ", "%20");\n</code></pre>\n\n<p>Again, nothing happens, instead you have to do:</p>\n\n<pre><code>inputText = inputText.Replace(" ","%20");\n</code></pre>\n\n<p>Now, if you understand that strings are immutable, that makes perfect sense. If you don\'t, then you are just confused. The proper place for <code>Replace()</code> would be where <code>format()</code> is, as a static method of <code>String</code>:</p>\n\n<pre><code> inputText = String.Replace(inputText, " ", "%20");\n</code></pre>\n\n<p>Now there is no question as to what\'s going on.</p>\n\n<p>The real question is, why did the authors of these frameworks decide that one should be an instance method, and the other static? In my opinion, both are more elegantly expressed as static methods.</p>\n\n<p>Regardless of your opinion, the truth is that you are less prone to make a mistake using the static version, and the code is easier to understand (No Hidden Gotchas).</p>\n\n<p>Of course there are some methods that are perfect as instance methods, take String.Length()</p>\n\n<pre><code>int length = "123".Length();\n</code></pre>\n\n<p>In this situation, it\'s obvious we are not trying to modify "123", we are just inspecting it, and returning its length. This is a perfect candidate for an instance method.</p>\n\n<p>My simple rules for Instance Methods on Immutable Objects:</p>\n\n<ul>\n<li>If you need to return a new instance of the same type, use a static method.</li>\n<li>Otherwise, use an instance method.</li>\n</ul>\n'}, {'answer_id': 31405706, 'author': 'догонят', 'author_id': 3199507, 'author_profile': 'https://Stackoverflow.com/users/3199507', 'pm_score': 2, 'selected': False, 'text': '<p>Both solutions workto simulate printf, but in a different way.\nFor instance, to convert a value to a hex string, you have the 2 following solutions:</p>\n\n<ul>\n<li><p>with <code>format()</code>, closest to <code>sprintf()</code>:</p>\n\n<pre><code>final static String HexChars = "0123456789abcdef";\n\npublic static String getHexQuad(long v) {\n    String ret;\n    if(v &gt; 0xffff) ret = getHexQuad(v &gt;&gt; 16); else ret = "";\n    ret += String.format("%c%c%c%c",\n        HexChars.charAt((int) ((v &gt;&gt; 12) &amp; 0x0f)),\n        HexChars.charAt((int) ((v &gt;&gt;  8) &amp; 0x0f)),\n        HexChars.charAt((int) ((v &gt;&gt;  4) &amp; 0x0f)),\n        HexChars.charAt((int) ( v        &amp; 0x0f)));\n    return ret;\n}\n</code></pre></li>\n<li><p>with <code>replace(char oldchar , char newchar)</code>, somewhat faster but pretty limited:</p>\n\n<pre><code>    ...\n    ret += "ABCD".\n        replace(\'A\', HexChars.charAt((int) ((v &gt;&gt; 12) &amp; 0x0f))).\n        replace(\'B\', HexChars.charAt((int) ((v &gt;&gt;  8) &amp; 0x0f))).\n        replace(\'C\', HexChars.charAt((int) ((v &gt;&gt;  4) &amp; 0x0f))).\n        replace(\'D\', HexChars.charAt((int) ( v        &amp; 0x0f)));\n    ...\n</code></pre></li>\n<li><p>There is a third solution consisting of just adding the char to <code>ret</code> one by one (char are numbers that <i>add to each other</i>!) such as in:</p>\n\n<pre><code>...\nret += HexChars.charAt((int) ((v &gt;&gt; 12) &amp; 0x0f)));\nret += HexChars.charAt((int) ((v &gt;&gt;  8) &amp; 0x0f)));\n...\n</code></pre></li>\n</ul>\n\n<p>...but that\'d be <strong>really</strong> ugly.</p>\n'}, {'answer_id': 57359951, 'author': 'armagedescu', 'author_id': 4582948, 'author_profile': 'https://Stackoverflow.com/users/4582948', 'pm_score': 1, 'selected': False, 'text': '<p>You can do a printf with a PrintStream to anything that is an OutputStream.\nSomehow like this, printing into a string stream:</p>\n<pre><code>PrintStream ps = new PrintStream(baos);\nps.printf(&quot;there is a %s from %d %s&quot;, &quot;hello&quot;, 3, &quot;friends&quot;);\nSystem.out.println(baos.toString());\n</code></pre>\n<p>This outputs following text <strong>there is a hello from 3 friends</strong>\nThe string stream can be created like this ByteArrayOutputStream:</p>\n<pre><code>ByteArrayOutputStream baos = new ByteArrayOutputStream();\n</code></pre>\n<p>You can accumulate many formats:</p>\n<pre><code>PrintStream ps = new PrintStream(baos);\nps.printf(&quot;there is a %s from %d %s &quot;, &quot;hello&quot;, 3, &quot;friends&quot;);\nps.printf(&quot;there are %d % from a %&quot;, 2, &quot;kisses&quot;, &quot;girl&quot;);\nSystem.out.println(baos.toString());\n</code></pre>\n<p>This outputs <strong>there is a hello from 3 friends there are 2 kisses from a girl</strong><br />\nCall reset on ByteArrayOutputStream to generate a clean new string</p>\n<pre><code>ps.printf(&quot;there is a %s from %d %s&quot;, &quot;flip&quot;, 5, &quot;haters&quot;);\nbaos.reset(); //need reset to write new string\nps.printf(&quot;there are %d % from a %&quot;, 2, &quot;kisses&quot;, &quot;girl&quot;);\nSystem.out.println(baos.toString());\n</code></pre>\n<p>The output will be <strong>there are 2 kisses from a girl</strong></p>\n'}, {'answer_id': 62706682, 'author': 'andreoss', 'author_id': 12462444, 'author_profile': 'https://Stackoverflow.com/users/12462444', 'pm_score': 3, 'selected': False, 'text': '<p>Since Java 13 you have <code>formatted</code> <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html#formatted(java.lang.Object...)" rel="noreferrer">1</a> method on String, which was added along with text blocks as a preview feature <a href="https://docs.oracle.com/en/java/javase/13/text_blocks/index.html#string-formattedobject...-args" rel="noreferrer">2</a>.\nYou can use it instead of <code>String.format()</code></p>\n<pre class="lang-java prettyprint-override"><code>Assertions.assertEquals(\n   &quot;%s %d %.3f&quot;.formatted(&quot;foo&quot;, 123, 7.89),\n   &quot;foo 123 7.890&quot;\n);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/47045', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4498/']