2

Hi I've searched but can't find the answer I'm looking for or I'm not reading it right.

I have a NSString I use

NSString *string1 = [infolist objectAtIndex:0];
NSUInteger len = [string1 length];

Is it possible to replace all characters not white spaces with say a * or some other unreadable character.

Example: this is a string to **** ** * ******

Vadim
  • 9,383
  • 7
  • 36
  • 58
mrgonuts
  • 56
  • 5
  • probable [duplicate](http://stackoverflow.com/q/668228/1367611) – Vidul Jun 02 '12 at 07:04
  • @ВидулПетров this isn't a duplicate. The link that you gave is quite far different from what mrgonuts is trying to do. He wants the whole string to be replaced. – Kimpoy Aug 01 '12 at 03:10

1 Answers1

9

Make use of regular expressions if you target OS X 10.7 and later:

NSString *originalString = @"This is a string";
NSString *nonspaceRegexp = @"\\S"; // = /\S/
NSStringCompareOptions options = NSRegularExpressionSearch;
NSRange replaceRange = NSMakeRange(0, originalString.length);
NSString *replacedString = [originalString
                            stringByReplacingOccurrencesOfString:nonspaceRegexp
                                                      withString:@"*"
                                                         options:options
                                                           range:replaceRange];
NSLog(@"%@", replacedString); // **** ** * ******
Vadim
  • 9,383
  • 7
  • 36
  • 58