Skip to content

Instantly share code, notes, and snippets.

@amomchilov
Created February 2, 2025 03:34
Show Gist options
  • Save amomchilov/fea0fb72fe851c6d2866fa298a15b625 to your computer and use it in GitHub Desktop.
Save amomchilov/fea0fb72fe851c6d2866fa298a15b625 to your computer and use it in GitHub Desktop.
let hello = "Hello"
let world = "World!"
print("\(hello, padLeft: 10) \(world, padLeft: 10)") // Spaces are the default padding
print("\(hello, padLeft: 10, with: "H") \(world, padLeft: 10, with: "W")")
extension String.StringInterpolation {
mutating func appendInterpolation(
_ str: String,
padLeft length: Int,
with padding: String = " "
) {
let paddingCount = length - str.count
if 0 < paddingCount {
appendLiteral(String(repeating: padding, count: paddingCount))
}
appendLiteral(str)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment