use*_*236 48 whitespace objective-c nsstring nsarray
假设我有一个像这样的字符串:
hello world this may have lots of sp:ace or little space
Run Code Online (Sandbox Code Playgroud)
我想将此字符串分隔为:
@"hello", @"world", @"this", @"may", @"have", @"lots", @"of", @"sp:ace", @"or", @"little", @"space"
Run Code Online (Sandbox Code Playgroud)
谢谢.
Vik*_*ica 80
NSString *aString = @"hello world this may have lots of sp:ace or little space";
NSArray *array = [aString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
array = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF != ''"]];
Run Code Online (Sandbox Code Playgroud)
输入此表格未经测试
dan*_*dee 22
我建议两步法:
NSArray *wordsAndEmptyStrings = [yourLongString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *words = [wordsAndEmptyStrings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]];
Run Code Online (Sandbox Code Playgroud)
Moh*_*hit 22
这对我有用
NSString * str = @"Hi Hello How Are You ?";
NSArray * arr = [str componentsSeparatedByString:@" "];
NSLog(@"Array values are : %@",arr);
Run Code Online (Sandbox Code Playgroud)
用块做这个很容易,尝试这样的事情:
NSString* s = @"hello world this may have lots of space or little space";
NSMutableArray* ar = [NSMutableArray array];
[s enumerateSubstringsInRange:NSMakeRange(0, [s length]) options:NSStringEnumerationByWords usingBlock:^(NSString* word, NSRange wordRange, NSRange enclosingRange, BOOL* stop){
[ar addObject:word];
}];
Run Code Online (Sandbox Code Playgroud)
NSString * mainString = @"Today is your day";
NSArray * array = [mainString componentsSeparatedByString:@" "];
NSLog(@"Expected string is : %@",array);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36578 次 |
| 最近记录: |