0

In Java having the following

printf("%-5s Hello %s %n", "", "There!")

It prints

     Hello There! 

Where exists 5 empty spaces located to the left.

Until here it works how is expected, but ...

I want to know if is possible: How avoid the "" declaration and change %-5s to something that printf can interpret that represents an empty space?

I mean: for example %-5e where e is assumed to represent an empty character and therefore is not necessary anymore declare "" explicitly.

Something similar like %n where it does not required an argument through the printf. Is clear that %s requires an argument, in this case "" but being curious if my request can be accomplished.

I read the Formatter javadoc for that required feature and seems does not exist.

I already did do a research on SO and some suggested solutions is use the + within printf, but to be honest it reduces readability

Therefore the goal is (formatted for better presentation):

From: printf("%-5s Hello %s %n", "", "There!")
To:   printf("%-5e Hello %s %n",     "There!") // %-5e is the functionality I need or looking for
5
  • All you want is a way to print 5 spaces before Hello There!. How about System.out.printf(" ".repeat(5) + " Hello %s %n", "There!");? Commented Jun 20, 2020 at 14:21
  • Is possible do that without use +? I don't want lost readability Commented Jun 20, 2020 at 14:31
  • 1
    In fact, even my previous comment is not applicable in your case as you are using Java-8 (I just saw one of your comments). In order to make it easier for future visitors to this question, I've tagged it with Java-8 now. Commented Jun 20, 2020 at 16:12
  • 2
    Is this really carrying the intended semantics? What about printf("%11s %s %n", "Hello", "There!")? Commented Jun 22, 2020 at 11:37
  • Your approach is totally valid, proceed to put it how the answer, to be marked by my side. Commented Jun 22, 2020 at 14:00

2 Answers 2

1

If you are using Java 12 or higher and all you need is to adjusts the indentation of the resulting string, you could use String#indent. Using your example to insert 5 spaces at the beginning:

System.out.printf("Hello %s %n".indent(5),  "There!");

Since indent also normalizes line termination characters, you could also remove %n

System.out.printf("Hello %s ".indent(1),  "There!");
System.out.printf("Hello %s ".indent(2),  "There!");
System.out.printf("Hello %s ".indent(3),  "There!");
System.out.printf("Hello %s ".indent(4),  "There!");

to get something like:

 Hello There! 
  Hello There! 
   Hello There! 
    Hello There! 
Sign up to request clarification or add additional context in comments.

Comments

0

You should define new printf like this.

static Pattern EMPTY_CONVERSION = Pattern.compile("%-?(\\d+)e");

public static String printf(String format, Object... args) {
    StringBuffer sb = new StringBuffer();
    Matcher m = EMPTY_CONVERSION.matcher(format);
    while (m.find())
        m.appendReplacement(sb, " ".repeat(Integer.parseInt(m.group(1))));
    m.appendTail(sb);
    return String.format(sb.toString(), args);
}

If you use Java 8, make your own repeat.

e is reserved for floating point conversions, so you might want to change it.

1 Comment

I understand, "e" was just a representation, could be any character ... "%anything"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.