1

I'm having issues getting printf style formatting with String(format:, ...) to work as I would expect it to work in Objective C or C.

Here's my code:

let name = "Bob"
let type = "Person"
let formattedString = String(format: "%*@", 30, "\(name)(\(type))")
print("\"\(formattedString)\"")

// Expected output: "                   Bob(Person)"
// Actual output:   "Bob(Person)"

What am I doing wrong?

I also tried (with the same unexpected result):

let formattedString = String(format: "%30@", "\(name)(\(type))")
let formattedString = String(format: "%-30@", "\(name)(\(type))")  // Padding on the other side
let formattedString = String(format: "%-*@", 30, "\(name)(\(type))")
Hyperbole
  • 3,917
  • 4
  • 35
  • 54

0 Answers0