1

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?

QueueOverFlow
  • 1,336
  • 5
  • 27
  • 48

3 Answers3

2

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

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
0

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

Community
  • 1
  • 1
piam
  • 653
  • 6
  • 13
  • thanks for reply. It is hard to predetermine all the special characters in my string, How I can remove all special symbol except + - and space? – QueueOverFlow Nov 10 '12 at 11:23
  • trim is wrong.. it trims you want to rm all from beginning end only – Daij-Djan Nov 10 '12 at 11:43
  • wait for me a bit, I won't give up – piam Nov 10 '12 at 11:48
  • @piam code is not working for me :( NSMutableCharacterSet *special = [NSMutableCharacterSet characterSetWithCharactersInString:@"+ -"]; [special formUnionWithCharacterSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]]; NSString *temp=@"gaurav 1!@#!#@! kant !@#!@#!@#12kestwal "; NSString *yourString = [temp stringByTrimmingCharactersInSet:special]; NSLog(@"%@",yourString); – QueueOverFlow Nov 10 '12 at 12:00
  • do you try my update one? please see the latest update, the code that you post I still see you use trimming – piam Nov 10 '12 at 12:03
  • @piam thanks for your help, your solution is good. but not 100 % accurate, few special symbol like Äî still there. – QueueOverFlow Nov 10 '12 at 12:18
  • @Gryphon Arggh.. almost there, but anyway your welcome and more than willing to help you :). – piam Nov 10 '12 at 12:29
0

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?

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • Thanks.. +1 for your perfect code, It removes all the unwanted Special Symbol – QueueOverFlow Nov 10 '12 at 12:19
  • it's unsafe -- very. it fails for multibyte chars. see the characterAtIndex docs – Daij-Djan Nov 10 '12 at 12:25
  • What do you mean that it fails for multiple chars? It throws an exception in the case that the range is outside string bounds, but with the for loop is guaranteed that this will not happen. – Ramy Al Zuhouri Nov 10 '12 at 12:38
  • not multiple chars but multiBYTE -- see the docs for characters of NSString – Daij-Djan Nov 10 '12 at 18:53
  • True, characterAtIndex returns an unichar, didn't see it.An implicit cast was made, so the new string could have been dirty of also some unwanted characters, but printed like ASCII character (with no risk of segmentation fault).I edited the code. – Ramy Al Zuhouri Nov 10 '12 at 19:20