从数组中替换字符串中的特殊字符

时间:2016-10-12 08:59:38

标签: ios objective-c special-characters

以下是我的代码:

NSString *str = [EpiC objectAtIndex:indexPath.row];
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\/"];
str = [[str componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @"\"];
NSLog(@"%@", str); 

显示错误如下: 这一行的未知逃逸序列:

NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\/"];

和意想不到的' @'在计划中,在这一行:

NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\"];

3 个答案:

答案 0 :(得分:2)

未知转义序列表示必须使用第二个反斜杠转义反斜杠。

NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\\/"];

额外的反斜杠也解决了意外'@'程序错误。

答案 1 :(得分:1)

喜欢

 NSString *str = @"FSJ\/6356232";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\\/"];
str = [[str componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @"\\"];
       NSLog(@"%@", str);

你得到了

的输出

enter image description here

答案 2 :(得分:-1)

请按照以下步骤确定为您工作。

 NSString *str = @"FSJ\/6356232";
 NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\\/"];
 str = [[str componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @"\\"];
       NSLog(@"%@", str);