Skip to content

Instantly share code, notes, and snippets.

@erica
Created November 24, 2015 15:57
Show Gist options
  • Save erica/efbe3e92669077349d5a to your computer and use it in GitHub Desktop.
Save erica/efbe3e92669077349d5a 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
let 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