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

50,680

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.

Share:
50,680
Sheehan Alam
Author by

Sheehan Alam

iOS, Android and Mac Developer. i can divide by zero.

Updated on June 08, 2020

Comments

  • Sheehan Alam
    Sheehan Alam almost 4 years

    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.