Sheehan Alam Sheehan Alam - 6 months ago 14
Objective-C Question

How to replace a character in NSString without inserting a space?

Let's assume I have the string

NSString* myString = @"Hello,";


How can I remove the comma without leaving a space? I have tried:

NSString* newString = [myString stringByReplacingOccurrencesOfString:@"," withString:@""];


and

NSString* newString = [myString stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];


But both are leaving spaces.

Answer

I just ran the following as a test

NSString * myString = @"Hello,";

NSString * newString = [myString stringByReplacingOccurrencesOfString:@"," withString:@""];

NSLog(@"%@xx",newString);

And I get 2010-04-05 18:51:18.885 TestString[6823:a0f] Helloxx as output. There is no space left.