Alex Rivera | Logout

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

Asked 2010-04-05T23:47:13.290
32

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.

Edit
Report

1 Answer

0
NSString *newString = [myString substringToIndex:5];

That will ensure that there are only 5 characters in the string, but beware that this will also throw an exception if there are not at least 5 characters in the string to begin with. How are you handling this string? Is it being displayed to the user? Is it being written to a file? The code that you posted does not reproduce the error, so perhaps you should post the code that you are having a problem with.

answered 2010-04-06T00:11:48.140

Your Answer