[英]convert NSString to an NSArray with more than one condition
I have a string that contains some text with periods and commas. 我有一个包含一些带有句点和逗号的文本的字符串。 I'd like to convert it to an NSArray based on the commas AND the periods.
我想根据逗号和句点将其转换为NSArray。 I use this method to separate based on one type of condition, but how do I do that with two?
我使用这种方法根据一种类型的条件进行分离,但是如何使用两种方法呢?
componentsSeparatedByString:
Thanks. 谢谢。
You can use componentsSeparatedByCharactersInSet:
. 您可以使用
componentsSeparatedByCharactersInSet:
For example: 例如:
NSString *str = @"1,2.3";
NSArray *arr = [str componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@",."]];
Please refer to NSString Class Reference . 请参考NSString类参考 。
You need to create NSCharacterSet
with a comma and a dot in it, and use this method: 您需要创建带有逗号和点的
NSCharacterSet
,然后使用此方法:
- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
Here is an example: 这是一个例子:
NSArray *arr = [str componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@",."]];
NSString *tempTextOut = [NSString stringWithFormat:@"jdnfj,adsfsajfhkj,hasdfjhdsjkfh,hdsufhajkdsfhk"];
NSMutableArray *array = [[tempTextOut componentsSeparatedByString: @","] mutableCopy];
NSLog(@"%@",array);
>>OutPut1 >>输出1
jdnfj,
adsfsajfhkj,
hasdfjhdsjkfh,
hdsufhajkdsfhk
and 和
NSString *tempTextOut = [NSString stringWithFormat:@"jdnfj,adsfsajfhkj,,,hasdfjhdsjkfh,,,,hdsufhajkdsfhk"];
NSMutableArray *array = [[tempTextOut componentsSeparatedByString: @",,,"] mutableCopy];
NSLog(@"%@",array);
>>OutPut2 >>输出2
"jdnfj,adsfsajfhkj",
hasdfjhdsjkfh,
",hdsufhajkdsfhk"
Hope,this will help you.. 希望这个能对您有所帮助..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.