开发者

NSString's enumerateSubstrings doesn't include symbols

开发者 https://www.devze.com 2023-02-14 01:50 出处:网络
When using NSString\'s enumerateSubstringsInRange:options:usingBlock: with the options set as NSStringEnumerationByWords it doesn\'t include symbols such as /* or // which should be treated similarly

When using NSString's enumerateSubstringsInRange:options:usingBlock: with the options set as NSStringEnumerationByWords it doesn't include symbols such as /* or // which should be treated similarly to words as they are seperated by spaces.

I also tried using NSStringEnumerat开发者_如何学PythonionByComposedCharacterSequences but it seems to do exactly the same thing even without this option, it simply goes through every single letter.

Is their no way to enumerate through every substring separated by a space? It sounds so simple by no way to do is provided to do this using enumerateSubstringsInRange:options:usingBlock:.

EDIT

I was also using the option NSEnumerationReverse to got through the substrings backwards.


You could use NSScanner for something like this. It's sort of the long way around, but if the enumerate... messages aren't doing it for you, it might be worth looking at.

For example, you could do something like

NSString *output = nil;
NSCharacterSet *whitespaceCharSet = [NSCharacterSet whitespaceCharacterSet];

NSScanner *scanner = [[NSScanner alloc] initWithString:someString];

// should skip leading whitespace and read everything up to the next whitespace
[scanner scanUpToCharactersFromSet:whitespaceCharSet intoSring:&output];

[scanner release];

Sort of a crude example, but the documentation for NSScanner is fairly simple.

Edit: Alternatively, you could do something like this:

NSString *someString = <...>; // get your string somehow

NSCharacterSet *charSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSArray *components = [someString componentsSeparatedByCharactersInSet:charSet];

[components
 enumerateObjectsWithOptions:NSEnumerationReverse
 usingBlock:^(id obj, NSUInteger index, BOOL *stop) {
        // do stuff
    }];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号