2012-11-26 57 views
4

我想知道是否有一種簡單的方法來查找字符(如'$')在字符串中出現的次數Objective-C的。

我現在用的是現實世界的例子是一個字符串,看起來像:

542$764$231$DataEntry 

我需要做的是第一:

1)計算時代的「$」顯示的金額瞭解DataEntry在我的數據庫中的層次(我的數據庫結構是我編寫的)

2)那麼我需要獲取所有數字,因爲它們是索引號。數字需要存儲在NSArray中。我會循環他們所有得到不同的指標。我不打算解釋我的數據庫結構如何工作,因爲這不相關。

基本上從那個NSString,我需要,'$'的次數出現了。所有美元符號之間的數字。這在PHP中很容易實現,但我很想知道如何在Objective-C中做到這一點。

感謝,

邁克爾

回答

4
[[@"542$764$231$DataEntry" componentsSeparatedByString:@"$"] count]-1 
+0

的感謝!有關我如何獲得$之間的數字的任何想法? –

+1

只需要忽略count調用:'NSArray * components = [str componentsSeparatedByString:@「$」];' –

+0

從組件數組中刪除第一個和最後一個對象。 –

1

componentsSeparatedByString通過@Parag Bafna和@J夏皮羅或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可能是首選的方法等等表示模式具有簡單的重複分隔符的性能;但爲了完整起見,我包含了這種方法。

+0

謝謝你的正則表達式的方法!這是非常信息!如果事情變得複雜,我一定會牢記這一點! –

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 
相關問題