Skip to content

Instantly share code, notes, and snippets.

@erica
Created November 24, 2015 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/c2ee1e2284e7aa3f7bae to your computer and use it in GitHub Desktop.
Save erica/c2ee1e2284e7aa3f7bae to your computer and use it in GitHub Desktop.
enum StringPaddingStyle {case Left, Right}
func padStringToLength(
sourceString: String,
destinationCount: Int,
paddingStyle: StringPaddingStyle = .Left,
paddingCharacter: Character = " "
) -> String {
let
padCount = destinationCount - sourceString.characters.count,
padString = String(count: padCount, repeatedValue: paddingCharacter)
return
paddingStyle == .Left
? padString + sourceString
: sourceString + padString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment