6

I want simply replace all occourrencies of "+" with a blank " " char... I tried some sample listed here, also used NSSMutableString, but the program crash... what's the best way to replace a char from another?? thanks

ghiboz
  • 7,863
  • 21
  • 85
  • 131

2 Answers2

13

If you want to replace with a mutable string (NSMutableString) in-place:

[theMutableString replaceOccurrencesOfString:@"+"
                                  withString:@" "
                                     options:0
                                       range:NSMakeRange(0, [theMutableString length])]

If you want to create a new immutable string (NSString):

NSString* newString = [theString stringByReplacingOccurrencesOfString:@"+"
                                                           withString:@" "];
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
1
NSString *firstString = @"I'm a noob at Objective-C", *finalString;

finalString = [[firstString stringByReplacingOccurrencesOfString:@"O" withString:@"0"] stringByReplacingOccurrencesOfString:@"o" withString:@"0"];

Got the code from here!

Arman Ortega
  • 3,003
  • 1
  • 30
  • 28
Bogdan Constantinescu
  • 5,296
  • 4
  • 39
  • 50