在这里,我试图从字符串中删除所有特殊字符,但除了删除单词之间的空格外,这种方法还可以,我需要单词之间的空格。

NSCharacterSet *charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet];

    dataString = [[dataString componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@""];

最佳答案

您可以像以下示例一样使用NSMutableCharacterSet:

NSMutableCharacterSet * charactersToRemove = [NSMutableCharacterSet alphanumericCharacterSet];
[charactersToRemove addCharactersInString:@" "];

NSCharacterSet *newCharactersToRemove = [charactersToRemove invertedSet];

07-28 01:29