我从“联系人”中导入电话号码并保存在中NSString。此字符串包含空格,我尝试使用方法将其删除:
NSString
numero = numero.stringByReplacingOccurrencesOfString(" ", withString: "")
这种方法行不通。
func sostituisci( stringa: NSString! ) -> NSString { var numero: NSString = "" NSLog(stringa) numero = ((stringa as String).stringByReplacingOccurrencesOfString(" ", withString: "") as NSString) NSLog(numero) return numero }
输出不变
日志
2014-11-05 17:54:50.734 HappyRicarica[33438:3119446] (327) 124-3503 2014-11-05 17:54:50.737 HappyRicarica[33438:3119446] (327) 124-3503
我怀疑您字符串中的空格字符实际上不是空格。请尝试在后面添加此代码,NSLog(string)以查看字符串中字符的unicode标量值是什么:
NSLog(string)
for uni in (stringa as String).unicodeScalars { println("\(uni) = \(uni.value)") }
“((327)124-3503)”的预期输出为:
( = 40 3 = 51 2 = 50 7 = 55 ) = 41 = 32 1 = 49 2 = 50 4 = 52 - = 45 3 = 51 5 = 53 0 = 48 3 = 51
从您的评论中,您的空间值为160而不是32。您可以使用以下方法将其删除:
numero = stringa.stringByReplacingOccurrencesOfString(String(Character(UnicodeScalar(160))), withString: "")