Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following Java class implements the most important features of the String.Format method in C#. It supports the 'X' and 'D' format specifiers, but not 'F', 'G', or 'R'.</p> <pre><code>package com.upokecenter.util; import java.util.Locale; public final class DotNetFormatter { private DotNetFormatter() { } public static String format(String formattedText, Object... options) { return format(Locale.getDefault(),formattedText,options); } public static String format(Locale locale, String formattedText, Object... options) { int numOptions = options.length; StringBuilder buffer = new StringBuilder(); int i; for (i = 0; i &lt; formattedText.length(); i++) { char c = formattedText.charAt(i); if (c == '{') { i++; if (i &lt; formattedText.length()) { c = formattedText.charAt(i); if (c == '{') { buffer.append('{'); } else if (c &gt;= '0' || c &lt;= '9') { int x = (c - '0'); i++; while (i &lt; formattedText.length()) { c = formattedText.charAt(i); if (c == ':') { i++; if (x &gt;= numOptions || i &gt;= formattedText.length()) throw new IllegalArgumentException( "Format string contains a badly numbered argument."); char formatType = formattedText.charAt(i); if (formatType == 'x' || formatType == 'X' || formatType == 'd' || formatType == 'D') { i++; if (i &gt;= formattedText.length()) throw new IllegalArgumentException( "Format string contains a badly numbered argument."); char formatCount = formattedText.charAt(i); if (formatCount == '}') { switch (formatType) { case 'x': buffer.append(String.format(locale, "%x", options[x])); break; case 'X': buffer.append(String.format(locale, "%X", options[x])); break; case 'd': buffer.append(String.format(locale, "%d", options[x])); break; case 'D': buffer.append(String.format(locale, "%d", options[x])); break; } break; } else if (formatCount &lt; '0' || formatCount &gt; '9' || (++i) &gt;= formattedText.length()) throw new IllegalArgumentException( "Format string contains a badly numbered argument."); else { if (formattedText.charAt(i) != '}') throw new IllegalArgumentException( "Format string contains a badly numbered argument."); String fmt = ""; switch (formatType) { case 'x': fmt = String.format("%%0%cx", formatCount); break; case 'X': fmt = String.format("%%0%cX", formatCount); break; case 'd': fmt = String.format("%%0%cd", formatCount); break; case 'D': fmt = String.format("%%0%cd", formatCount); break; } buffer.append(String.format(locale, fmt, options[x])); break; } } else { throw new IllegalArgumentException( "Format string contains a badly formatted argument."); } } else if (c == '}') { if (x &gt;= numOptions) throw new IllegalArgumentException( "Format string contains a badly numbered argument."); buffer.append(String.format(locale, "%s", options[x])); break; } else if (c &gt;= '0' || c &lt;= '9') { i++; x = x * 10 + (c - '0'); } else { throw new IllegalArgumentException( "Format string contains a badly formatted argument."); } } } else { buffer.append('{'); buffer.append(c); } } else { buffer.append(c); } } else { buffer.append(c); } } return buffer.toString(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload