Created
January 6, 2015 16:33
-
-
Save ttepasse/5c8cdf4516700e49e1a4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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