Reputation: 11098
I have a UILabel with the following text:
Medium, Black
What I intended to do was grab the words in the string and insert each into a mutable array so I could use each title later on to identify something.
Here's how I done this:
NSMutableArray *chosenOptions = [[[[cell tapToEditLabel] text] componentsSeparatedByString: @" "] mutableCopy];
NSString *size = [chosenOptions objectAtIndex:0];
NSString *colour = [chosenOptions objectAtIndex:1];
I've logged these two NSString and size is returning "Medium," and colour is correctly returning "Black".
My comparison result is always false because of the comma:
itemExists = [[item colour] isEqualToString:colour] && [[item size] isEqualToString:size] ? YES : NO;
That comma causes itemExists to always equal NO.
Would appreciate a simple solution in code please.
The solution needs to only strip commas and not other characters. When dealing with clothing sizes for females I use sizes in a string like this: "[8 UK]" so remove non-alphanumeric characters would remove these. So I really need a solution to deal with just the commas.
Thanks for your time.
Upvotes: 2
Views: 2898
Reputation: 726509
Rather than splitting on spaces, you could split on spaces or commas, like this:
NSMutableArray *chosenOptions = [[[[cell tapToEditLabel] text] componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@" ,"]] mutableCopy];
[chosenOptions removeObject:@""];
This would eliminate commas from the size
and colour
strings.
Upvotes: 5
Reputation: 4630
[yourString stringByReplacingOccurrencesOfString:@"," withString:@""];
easy squeezy lemon peesey
Upvotes: 2
Reputation: 172408
Try this:
NSString * myString = @"Medium, Black";
NSString * newString = [myString stringByReplacingOccurrencesOfString:@", " withString:@""];
NSLog(@"%@xx",newString);
Upvotes: 1