Skip to content

Instantly share code, notes, and snippets.

@ttepasse
Created January 6, 2015 16:33
Show Gist options
  • Save ttepasse/5c8cdf4516700e49e1a4 to your computer and use it in GitHub Desktop.
Save ttepasse/5c8cdf4516700e49e1a4 to your computer and use it in GitHub Desktop.
func uglyFirstLetterTransformation(str: String) -> String? {
if str.isEmpty {
return .None
}
let Letters = NSCharacterSet.letterCharacterSet()
let Numbers = NSCharacterSet.decimalDigitCharacterSet()
let firstLetter : NSString = str.substringToIndex( advance(str.startIndex, 1) )
let flAsUniChar : unichar = firstLetter.characterAtIndex(0)
var result = ""
switch (flAsUniChar) {
case 223: // Sonderbehandlung fürs ß. 223 ist dessen Wert als unichar
result = "S"
case let c where Letters.characterIsMember(c):
let base = firstLetter.stringByFoldingWithOptions(NSStringCompareOptions.DiacriticInsensitiveSearch,
locale: NSLocale.currentLocale())
result = base.uppercaseString
case let c where Numbers.characterIsMember(c):
result = firstLetter
default:
result = "&!"
}
return result
}
let testCases = [
"1 Test", // Zahl
"x Test", // Buchstabe
"ß Test", // ß
"ä Test", // Umlaut
". Test", // Punktuation
" Test", // Whitespace
"" // Empty
]
for test in testCases {
if let result = uglyFirstLetterTransformation(test) {
print(test)
print(" -> ")
println(result)
} else {
println("nil")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment