locoboy
locoboy

Reputation: 38920

Putting comma between each word in NSString

I have an NSString that is like @"nice car" and would like to create an NSString that is @"nice,car". Is there a way to do this?

Upvotes: 1

Views: 152

Answers (1)

John Parker
John Parker

Reputation: 54435

You could use the NSString stringByReplacingOccurrencesOfString:withString: method to achieve this as follows:

NSString *stringWithSpaces = @"nice car";
NSString *stringWithCommas = [stringWithSpaces stringByReplacingOccurrencesOfString:@" " withString:@","];

Upvotes: 3

Related Questions