4

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

4 回答 4

4

我怀疑您的字符串中的空格字符并不是真正的空格。尝试在之后添加它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: "")
于 2014-11-05T17:58:05.213 回答
2

更新:Xcode 7.2 • Swift 2.1.1

extension String {
    var numbersOnly: String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "1234567890")
            .invertedSet)
            .joinWithSeparator("")
    }
    var numbersExempt: String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "1234567890"))
            .joinWithSeparator("")
    }
    var charactersOnly: String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").invertedSet).joinWithSeparator("")
    }
    var charactersExempt: String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")).joinWithSeparator("")
    }
    func keep(keepIt: String) -> String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: keepIt).invertedSet).joinWithSeparator("")
    }
    func exclude(excludeIt: String) -> String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: excludeIt)).joinWithSeparator("")
    }
}

let phoneNumber = "+1 (555) 555 - 5555".numbersOnly
print(phoneNumber) // "15555555555"

let excludePlusMinus = "+1 (555) 555-5555".exclude("+-") // "1 (555) 5555555"
let keepWhatever = "+1 (555) 555-5555".keep("()-+") //  "+()-"

您还可以使用您的函数通过一些调整仅减去空格。试试这样:

func sostituisci(stringa: String) -> String {
     return stringa.stringByReplacingOccurrencesOfString(" ", withString: "")
}

sostituisci("1 234 567 8901")   // "12345678901"

或者像一个扩展:

extension String {
    var sostituisci: String {
        return stringByReplacingOccurrencesOfString(" ", withString: "")
    }
}
let phoneNumber2 = "1 234 567 8901".sostituisci
于 2014-11-05T18:16:24.970 回答
0

你试过了吗 :

let str = "XX XX XX XX XX"
    let separated = str.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    let finalStr = "".join(separated)
于 2014-11-05T14:39:42.817 回答
0

NSString不是String,所以你应该使用:

numero = ((numero as String).stringByReplacingOccurrencesOfString(" ", withString: "") as NSString)
于 2014-11-05T14:47:47.880 回答