Count the number of times '$' displayed in a string (Objective C)
I was interested to know if there is an easy way to find the number of times the # $ character appears in a string in objective-c.
An example of the real world that I use is a line that would look like this:
542$764$231$DataEntry I need to first:
1) count the number of times “$” is displayed to find out what level of DataEntry is in my database (my database structure is the one I compiled)
2), then I need to get all the numbers, since they are index numbers. Figures should be stored in NSArray. And I will go through them, getting different indexes. I will not explain how my database structure works, as it does not matter.
Mostly from this NSString, I need, the number of times "$" appears. And all the numbers between the dollar signs. This would be an easy step in PHP, but I was curious to see how I can do this in objective-c.
Thanks,
Michael
componentsSeparatedByString suggested by @Parag Bafna and @J Shapiro or NSRegularExpression for example:
#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 is probably the preferred approach and much more advanced when the template has simple repeating separators; but I have included this approach for completeness.
Try this code:
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 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