Most of these answers explain what %n does (which is to print nothing and to write the number of characters printed thus far to an int variable), but so far no one has really given an example of what use it has. Here is one:
int n;
printf("%s: %nFoo\n", "hello", &n);
printf("%*sBar\n", n, "");
will print:
hello: Foo
Bar
with Foo and Bar aligned. (It's trivial to do that without using %n for this particular example, and in general one always could break up that first printf call:
int n = printf("%s: ", "hello");
printf("Foo\n");
printf("%*sBar\n", n, "");
Whether the slightly added convenience is worth using something esoteric like %n (and possibly introducing errors) is open to debate.)
Most of these answers explain what %n does (which is to print nothing and to write the number of characters printed thus far to an int variable), but so far no one has really given an example of what use it has. Here is one:
int n;
printf("%s: %nFoo\n", "hello", &n);
printf("%*sBar\n", n, "");
will print:
hello: Foo
Bar
with Foo and Bar aligned. (It's trivial to do that without using %n for this particular example, and in general one always could break up that first printf call:
int n = printf("%s: ", "hello");
printf("Foo\n");
printf("%*sBar\n", n, "");
Whether the slightly added convenience is worth using something esoteric like %n (and possibly introducing errors) is open to debate.)
Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.
#include <stdio.h>
int main()
{
int val;
printf("blah %n blah\n", &val);
printf("val = %d\n", val);
return 0;
}
The previous code prints:
blah blah
val = 5
Videos
Checkout the following article on MSDN about examples of the N format. This is also covered in the Standard Numeric Format Strings article.
Relevant excerpts:
// Formatting of 1054.32179:
// N: 1,054.32
// N0: 1,054
// N1: 1,054.3
// N2: 1,054.32
// N3: 1,054.322
When precision specifier controls the number of fractional digits in the result string, the result string reflects a number that is rounded to a representable result nearest to the infinitely precise result. If there are two equally near representable results:
- On the .NET Framework and .NET Core up to .NET Core 2.0, the runtime selects the result with the greater least significant digit (that is, using MidpointRounding.AwayFromZero).
- On .NET Core 2.1 and later, the runtime selects the result with an even least significant digit (that is, using MidpointRounding.ToEven).
This is where the documentation is:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
The numeric ("N") format specifier converts a number to a string of the form "-d,ddd,ddd.ddd…", where "-" indicates a negative number symbol if required, "d" indicates a digit (0-9) ...
And this is where they talk about the default (2):
http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numberdecimaldigits.aspx
// Displays a negative value with the default number of decimal digits (2).
Int64 myInt = -1234;
Console.WriteLine( myInt.ToString( "N", nfi ) );
Having \n in a format string is just fine.
Perhaps you should try a platform specific new-line. With format strings you can use %n.
That is, try the following:
String.format(Dic.message.replace("\\n", "%n"), interventionSize, userId);
Alternatively you could use System.lineSeparator(). This will ensure platform independence. If you use a JDK before 1.7 its System.getProperty("line.separator");.
Code would then look like this:
String.format(Dic.message.replace("\\n", System.lineSeparator()), interventionSize, userId);
or this (for JDK before 1.7):
String.format(Dic.message.replace("\\n", System.getProperty("line.separator")), interventionSize, userId);
In the context of strings:
%n and \n both make newline characters. I have made some test code to see if there is any difference between using %n and \n other than that %n can only be used when formatting strings, and I don't see anything:
public class Practice {
public static String stuff () {
String x = "blah";
return "blah blah blah \n" + //Starts us off
String.format("%s %s %s%n", x, x, x) + //Using %n to do newline
String.format("%s %s %s\n", x, x, x) + // Using \n to do newline
"blah blah blah \n"; //Checks if newline was made
}
public static void main(String args[]) {
System.out.print(stuff());
}
The comments that I made inside the stuff() method should explain how the program works. Is there really any difference between %n and \n?