2014-04-07 6 views
0

나는 MFMailComposeViewController을 사용하여 전자 메일의 첨부 파일 작업을하고 있으며 pdf의 파일 이름은 이벤트 이름에 대한 사용자의 입력을 기반으로합니다. 존 웨딩, 또는 존의 결혼식.NSString에서 파일 이름에 대한 특수 문자 제거하기 PDF에 첨부 된 PDF 파일

파일 이름에서 특수 문자를 제거하고 싶습니다. 내 코드에서

, 내가이 짓을했는지 : 요한의 결혼식의 경우이와

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"'#%^&{}[]~|\/?.<," options:0 error:NULL]; 
NSString *string = self.occasion.title; 
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""]; 

NSString *fileName = [NSString stringWithFormat:@"(%@).pdf", modifiedString]; 

가, 내 이름에서 아포스트로피를 제거하지만 나는 거기에 해시 또는 다른 기호를 사용한다면 , 파일 이름에있는 파일은 제거하지 않습니다.

온라인으로 스택 오버플로 예제를 보았지만 모두 매우 복잡해 보입니다. 파일 이름에서 제거 할 기호를 정확히 알고 있습니다.

모든 안내는 정말 감사하겠습니다.

텍스트 : 넣어 밖으로

+0

u는 그냥에서 파일 –

+0

의 모든 특수 문자를 제거하려면 예를 들어 파일 이름이 John의 W # dding 및 % ^. pdf 인 경우 Johns Wedding.pdf – amitsbajaj

+0

을 제외한 파일 이름의 모든 특수 문자를 제거하려면 허용 된 문자의 흰색 목록을 사용하는 것이 좋습니다 – gwillie

답변

2
NSString *textString = @"abcd334%$^%^%80)(*^ujikl"; 

//this is for remove the specified characters 
NSCharacterSet *chs = [NSCharacterSet characterSetWithCharactersInString:@"'#%^&{}[]/~|\?.<,"]; 
NSString *resultString = [[textString componentsSeparatedByCharactersInSet:chs] componentsJoinedByString:@""]; 

//this is for get the specified characters 
NSCharacterSet *chs1 = [[NSCharacterSet characterSetWithCharactersInString:@"'#%^&{}[]/~|\?.<,"] invertedSet]; 
NSString *resultString1 = [[textString componentsSeparatedByCharactersInSet:chs1] componentsJoinedByString:@""]; 
NSLog(@"tex : %@",resultString); 
NSLog(@"reverse string : %@",resultString1); 

abcd334 $ (80)) (* ujikl

역 문자열 : %^%^%^