Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a char into NSString

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

like image 304
ghiboz Avatar asked Jun 29 '10 08:06

ghiboz


People also ask

How do you replace a character in a string in Objective C?

To replace a character in objective C we will have to use the inbuilt function of Objective C string library, which replaces occurrence of a string with some other string that we want to replace it with.

What is a Nsstring?

A static, plain-text Unicode string object which you use when you need reference semantics or other Foundation-specific behavior.


2 Answers

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:@" "];
like image 87
kennytm Avatar answered Oct 21 '22 06:10

kennytm


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!

like image 38
Bogdan Constantinescu Avatar answered Oct 21 '22 07:10

Bogdan Constantinescu