What is the equivalent of the "setw" stream manipulator for C++ in Java?

I've been messing with the java.util.formatter class, but haven't figured this out yet?

Recommended Answers

All 2 Replies

Generally it's the number between the percent sign ('%') and the letter. ...much as you would see in the C 'printf' function.

StringBuilder sb = new StringBuilder();
		Formatter formatter = new Formatter(sb, Locale.US);
		formatter.format("/%10d/", 23421);

Produces this output: / 23421/

Or you could directly use C style printf method.

commented: Good answer. +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.