0

I want to remove Special Characters, Numbers and Double Spaces from a string, but it's removing all spaces. How can I fix it?

Code:

_tfName.text = [[_tfName.text componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""];

Strings:

Real String          Formated          What I want
___________          ________          ___________
Lincon  Man          LiconMan          Licon Man
Name Surname         NameSurname       Name Surname
09123721*)(%!@ˆ*#    *blank*           *blank*
Luciano Nascimento
  • 2,600
  • 2
  • 44
  • 80
  • 1
    you are using a method that is not for your requirement. you need to search for regex. 100s of answer you can find here – Anoop Vaidya May 24 '13 at 17:15
  • 1
    Check out http://stackoverflow.com/questions/713918/replace-multiple-characters-in-a-string-in-objective-c – savner May 24 '13 at 17:19
  • @LucianoNascimento No, that link is for Objective-C. The question begins by referencing something in PHP but then the question asks how to do it in Objective-C. All of the answers are in Objective-C. – rmaddy May 24 '13 at 17:49
  • I suggest you do it in two steps. First string out all undesired characters and numbers. Then replace all multi-spaces with a single space. – rmaddy May 24 '13 at 17:50

3 Answers3

1

You have a lot of options available. A good one is to use NSRegularExpression for the two needs. One to find unwanted characters and remove them and another to find double spaces and remove them.

I recommend this option because you can reuse most of the logic for doing both.

You can also use NSScanner.

You could accomplish the unwanted characters part by using NSCharacterSet to create a set of chars you don't want and remove them with NSString method – stringByTrimmingCharactersInSet: that takes an NSCharacterSet argument.

NSString has – stringByReplacingOccurrencesOfString:withString: You can pass it a double space string.

Other options exist, but NSRegularExpression is the way to go for the double spaces. The NSCharacterSet approach is pretty easy for the unwanted characters.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
0

Did you think of passing a white space as the parameter to componentsJoinedByString:

string = [[string componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@" "];

Mohannad A. Hassan
  • 1,650
  • 1
  • 13
  • 24
0

This may be what you are looking for:

NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:@"[@#$.,!\\d]" options:0 error:nil];
NSString *input = @"1234 Nöel* *Smith $!@";
NSString *output = [re stringByReplacingMatchesInString:input 
                                                options:0 
                                                  range:NSMakeRange(0, [input length]) 
                                           withTemplate:@""];
/* -> Nöel Smith */

Depending if you know what characters to remove or which ones to keep, you can write the regexp like this or the opposite with [^caracters-to-keep]. Just use the needed metacharacters for your case.

djromero
  • 19,551
  • 4
  • 71
  • 68