문제

I am working with attachments in emails using MFMailComposeViewController and the file name of the pdf is based on the user's input for the event names, so this could be John Wedding, or John's Wedding.

I want to remove any special characters from the name of the file.

In my code, I've done this:

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];

With this, it removes the apostrophe from my filename if it's John's Wedding, but if I have a hash or any of the other symbols in there, it doesn't remove those in the file name.

I've seen a few stack overflow examples online but they all look extremely complicated; I know exactly which symbols I want to remove from the file name.

Any guidance would be really appreciated.

도움이 되었습니까?

해결책

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);

out put:

text : abcd334$80)(*ujikl

reverse string : %^%^%^

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top