2016-10-12 79 views
0

下面替換特殊字符是我的代碼:從字符串從一個數組

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:@"\"]; 
+0

你能證明你的字符串'str' –

+0

@ Anbu.Karthik FSJ \/6356232這是字符串,我想這是FSJ \ 6356232 –

回答

2

未知的轉義序列表示您必須使用第二個反斜槓來反轉反斜槓。

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

的反斜線解決意外「@」在程序錯誤了。

+0

太感謝你了,它的工作:) –

1

不喜歡

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

你的

enter image description here

-1

輸出做如下我爲你肯定的工作。

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

有什麼區別你的答案和我的回答 –

+0

對不起,但我沒有顯示你的答案,而我會給出答案 – iMHitesh

+0

我不會承諾downvote兄 –