4

我想知道是否有一种简单的方法可以找到诸如“$”之类的字符在objective-c语言的字符串中出现的次数。

我使用的真实世界示例是一个字符串,如下所示:

542$764$231$DataEntry

我需要做的是首先:

1)计算'$'出现的次数,以了解DataEntry在我的数据库中的哪一层(我的数据库结构是我编造的)

2)然后我需要获取所有数字,因为它们是索引号。这些数字需要存储在 NSArray 中。我将遍历它们以获得不同的索引。我不会解释我的数据库结构是如何工作的,因为这无关紧要。

基本上从那个 NSString,我需要,'$' 出现的次数。以及美元符号之间的所有数字。在 PHP 中这将是一件轻而易举的事,但我很想知道如何在 Objective-C 中实现这一点。

谢谢,

迈克尔

4

3 回答 3

4
[[@"542$764$231$DataEntry" componentsSeparatedByString:@"$"] count]-1
于 2012-11-26T15:03:21.177 回答
1

@Parag componentsSeparatedByStringBafna 和 @J Shapiro 建议的或NSRegularExpression例如:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSError *error = NULL;
        NSString *searchText = @"542$764$231$DataEntry";
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d{3})\\$" options:NSRegularExpressionCaseInsensitive error:&error];
        NSUInteger numberOfMatches = [regex numberOfMatchesInString:searchText options:0 range:NSMakeRange(0, [searchText length]) ];

        printf("match count = %ld\n",numberOfMatches);
        [regex enumerateMatchesInString:searchText 
                                options:0 
                                  range:NSMakeRange(0,[searchText length]) 
                             usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
            NSRange range = [match rangeAtIndex:1];
            printf("match = %s\n",[[searchText substringWithRange:range] UTF8String]);
        }];     
    }
}

在模式具有简单的重复定界符的情况下,这componentsSeparatedByString可能是首选方法并且性能更高;但为了完整起见,我包括了这种方法。

于 2012-11-26T15:20:06.113 回答
0

试试这个代码:

    NSMutableArray* substrings=[NSMutableArray new];
    // This will contain all the substrings
    NSMutableArray* numbers=[NSMutableArray new];
    // This will contain all the numbers
    NSNumberFormatter* formatter=[NSNumberFormatter new];
    // The formatter will scan all the strings and estabilish if they're
    // valid numbers, if so it will produce a NSNumber object
    [formatter setNumberStyle: NSNumberFormatterDecimalStyle];
    NSString* entry= @"542$764$231$DataEntry";
    NSUInteger count=0,last=0;
    // count will contain the number of '$' characters found
    NSRange range=NSMakeRange(0, entry.length);
    // range is the range where to check
    do
    {
        range= [entry rangeOfString: @"$" options: NSLiteralSearch range: range];
        // Check for a substring
        if(range.location!=NSNotFound)
        {
            // If there's not a further substring range.location will be NSNotFound
            NSRange substringRange= NSMakeRange(last, range.location-last);
            // Get the range of the substring
            NSString* substring=[entry substringWithRange: substringRange];
            [substrings addObject: substring];
            // Get the substring and add it to the substrings mutable array
            last=range.location+1;
            range= NSMakeRange(range.location+range.length, entry.length-range.length-range.location);
            // Calculate the new range where to check for the next substring
            count++;
            // Increase the count
        }
    }while( range.location!=NSNotFound);
    // Now count contains the number of '$' characters found, and substrings
    // contains all the substrings separated by '$'
    for(NSString* substring in substrings)
    {
        // Check all the substrings found
        NSNumber* number;
        if([formatter getObjectValue: &number forString: substring range: nil error: nil])
        {
            // If the substring is a valid number, the method returns YES and we go
            // inside this scope, so we can add the number to the numbers array
            [numbers addObject: number];
        }
    }
    // Now numbers contains all the numbers found
于 2012-11-26T15:36:07.743 回答