简体   繁体   中英

Removing All special character except + - operator from NSString

I want to remove all special character except +, space and - . How it is possible?

My string is

Äî;a[bRa]Co-Founder MobÄ: +91 9711700008

output:

abRaCo-Founder Mob +91 9711700008

According to Answer below, I update my code but still no success.

NSMutableCharacterSet *special = [NSMutableCharacterSet characterSetWithCharactersInString:@"+ -"];
[special formUnionWithCharacterSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]];


NSString *temp=@"gaurav 1!@#!#@! kant !@#!@#!@#12kestwal ";

NSString *yourString = [temp stringByTrimmingCharactersInSet:special];

NSLog(@"%@",yourString);

I missing anything?

much shorter than the mega func. :D above:

id s = @"$bla++$bleb+$blub-$b";
NSMutableCharacterSet *ch = [[NSCharacterSet alphanumericCharacterSet] mutableCopy];
[ch invert];
[ch removeCharactersInString:@"+-"]; //make sure its not removed
s = [[s componentsSeparatedByCharactersInSet:ch] componentsJoinedByString:@""];
NSLog(@"%@ - %@",ch, s);

in this example the $ is removed

could you please try,

NSString *yourString = @"Äî;a[bRa]Co-Founder MobÄ: +91 9711700008"

// thanks for Daij-Djan to point my mistake here
NSMutableCharacterSet *special = [[NSCharacterSet alphanumericCharacterSet] mutableCopy];
[special invert];
[special removeCharactersInString:@"+ -"]; 

// for this one I answer before Daij-Djan, please see edit version
//  note: I didn't offensive to anyone, please do not misunderstanding, thanks.
NSString *yourString =[[yourString componentsSeparatedByCharactersInSet:special]
 componentsJoinedByString:@""];

So, it separated and then join, thanks for this link Strip Non-Alphanumeric Characters from an NSString

This is an example of how to achieve what you're trying to do:

#import <Foundation/Foundation.h>

BOOL isWanted(unichar character)
{
    if(character>='a' && character<='z')
        return YES;
    if(character>='A' && character<='Z')
        return YES;
    if(character>='0' && character<='9')
        return YES;
    if(character=='-' || character=='+')
        return YES;
    return NO;
}

int main(int argc, char** argv)
{
    @autoreleasepool
    {
        NSString* string= @"Äî;a[bRa]Co-Founder MobÄ: +91 9711700008";
        NSMutableString* filteredString=[NSMutableString new];
        for(NSUInteger i=0; i<[string length];i++)
        {
            unichar character=[string characterAtIndex: i];
            if(isWanted(character))
                [filteredString appendString: [NSString stringWithFormat: @"%c",character]];
        }
        NSLog(@"%@",filteredString);
    }
    return 0;
}

The isWanted function returns YES if you want to have that character in string.For legibility I didn't use a lot of logic OR's but just if's, you can replace that with an unique if.
Sometimes if watching the documentation takes too much time, you can invent the solution yourself.Many people will disagree, of course a solution with a single line is better, but if you don't find the right documented method why losing time?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM