ios - 字符串替换电话号码iOS swift

标签 ios iphone swift nsstring import-contacts

我从“联系人”导入电话号码并保存在 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 标量值:

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: "")

关于ios - 字符串替换电话号码iOS swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26759768/

相关文章:

javascript - 在 webview-iphone 中通过 javascript 清除 cookie

ios - 应用程序内购买不起作用 Swift

iOS 键盘在静态表格 View 单元格中覆盖了一些 UITextField

ios - -[SKScene didBeginContact] 执行多次

ios - 如何使用 Xamarin iOS TouchRunner 在 UI 线程上对异步方法进行单元测试

ios - Swift 中的约束不起作用

iphone - 如何将 wifi 网络添加到 iOS 的已知网络列表中?

iphone - iOS 中的像素和点是什么?

swift AV基金会: QRcode scanning error in bright ambient

ios - 在前台推送通知