在NSString中替换智能引号(“) - 替换不会捕获它

时间:2016-09-27 16:10:18

标签: objective-c macos plugins

我尝试在Objective-C代码中捕获并替换智能引号,但都没有成功。我试图使用stringByReplacingOccurrencesOfString,它似乎没有抓住它。我试图用unicode值和ASCII字符捕获它。

输入的一个例子是:

  

无法完成操作 - 您无权保存   文件“桌面”中的文件“测试”。

这是我现有的函数,试图清理各种NSString值:

NSString* ScrubStringForInvalidCharacters(NSString* buffer)
{
    NSString *result = buffer;
    NSLog(@"%@",result);
    result = [result stringByReplacingOccurrencesOfString:@"\u2013" withString:@"-"];
    result = [result stringByReplacingOccurrencesOfString:@"\u2013" withString:@"-"];
    result = [result stringByReplacingOccurrencesOfString:@"\u2014" withString:@"-"];
    result = [result stringByReplacingOccurrencesOfString:@"\u2015" withString:@"-"];
    result = [result stringByReplacingOccurrencesOfString:@"\u2017" withString:@"_"];
    result = [result stringByReplacingOccurrencesOfString:@"\u2018" withString:@"\'"];
    result = [result stringByReplacingOccurrencesOfString:@"\u2019" withString:@"\'"];
    result = [result stringByReplacingOccurrencesOfString:@"\u201a" withString:@","];
    result = [result stringByReplacingOccurrencesOfString:@"\u201b" withString:@"\'"];
    result = [result stringByReplacingOccurrencesOfString:@"\u201c" withString:@"\""];
    result = [result stringByReplacingOccurrencesOfString:@"\u201d" withString:@"\""];
    result = [result stringByReplacingOccurrencesOfString:@"\u201e" withString:@"\""];
    result = [result stringByReplacingOccurrencesOfString:@"\u2026" withString:@"..."];
    result = [result stringByReplacingOccurrencesOfString:@"\u2032" withString:@"\'"];
    result = [result stringByReplacingOccurrencesOfString:@"\u2033" withString:@"\""];
    result = [result stringByReplacingOccurrencesOfString:@"“" withString:@"\""];
    result = [result stringByReplacingOccurrencesOfString:@"”" withString:@"\""];
    NSLog(@"%@",result);
    return result;
}

以下是NSLogs的回报:

  

2016-09-27 09:00:30.811 ... [3388:183176]无法完成操作 - 您无权将文件“Test”保存在“桌面”文件夹中。

     

2016-09-27 09:00:30.812 ... [3388:183176]无法完成操作 - 您无权将文件“Test”保存在“桌面”文件夹中。

如果我最终没有捕获并替换智能引号,我的应用程序最终会显示:

Error Message on Application

使用NSFileManager createDirectoryAtPath时会生成此错误。然后我捕获一个错误并将localizedDescription发送到ScrubStringForInvalidCharacters函数。

谢谢! - 杰夫

0 个答案:

没有答案