Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 530
Sheehan Alam Avatar asked Apr 05 '10 23:04

Sheehan Alam


People also ask

How do you escape in NSString?

This will escape double quotes in NSString: NSString *escaped = [originalString stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];

What is a NSString?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.


1 Answers

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.

like image 142
Brandon Bodnar Avatar answered Oct 09 '22 21:10

Brandon Bodnar