Created
February 2, 2025 03:34
-
-
Save amomchilov/fea0fb72fe851c6d2866fa298a15b625 to your computer and use it in GitHub Desktop.
Read more here: https://www.hackingwithswift.com/articles/178/super-powered-string-interpolation-in-swift-5-0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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