0

I am trying to print a string in formatted like " FOO" in Swift 4. I can do it with chars but I can't do it with a string object.

I tried:

let a = String(format: "%28c", 0x31)
print (a)
// prints "                           1"

But this only works with chars, not with strings.

How can I get the same result with strings?

Emre A
  • 71
  • 7
  • 3
    The `%@` format does not support a field width. See https://stackoverflow.com/questions/32338137/padding-a-swift-string-for-printing for various workarounds. – Martin R Sep 21 '18 at 15:20
  • 1
    Possible duplicate of [Padding a swift String for printing](https://stackoverflow.com/questions/32338137/padding-a-swift-string-for-printing) – Dávid Pásztor Sep 21 '18 at 15:35
  • 1
    FYI - I opened a bug report with Apple in February of 2013 to add support for a width specifier for `%@`. It was closed as a duplicate and the duplicate is still open. 5.5 years and still waiting for this feature. – rmaddy Sep 21 '18 at 16:36

0 Answers0