[英]How do I easily strip just a comma from an NSString?
I have a UILabel with the following text: 我有一个带有以下文字的UILabel:
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". 我已经记录了这两个NSString,并且大小正在返回“Medium”,并且颜色正确返回“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. 该逗号导致itemExists始终等于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.
当处理女性的服装尺寸时,我使用字符串中的尺寸:“[8 UK]”因此删除非字母数字字符会删除这些。 So I really need a solution to deal with just the commas.
所以我真的需要一个解决方案来处理逗号。
Thanks for your time. 谢谢你的时间。
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. 这将消除
size
和colour
字符串中的逗号。
[yourString stringByReplacingOccurrencesOfString:@"," withString:@""];
容易挤柠檬皮
Try this: 尝试这个:
NSString * myString = @"Medium, Black";
NSString * newString = [myString stringByReplacingOccurrencesOfString:@", " withString:@""];
NSLog(@"%@xx",newString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.