Reputation: 726
i need to know how meny image's i have from the file name exp: i have images call: first file: Splash_10001.jpg last file: Splash_10098.jpg and i want to inset then to array..
for(int i = 1; i <= IMAGE_COUNT; i++)
{
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%04d.%@",self.firstImageName,i,self.imageType]];
NSLog(@"%d",i);
[imgArray addObject:image];
}
i want to replace IMAGE_COUNT with number 98 but i need to get the numbre from the string the user send me : Splash_10098.jpg i need to Separate the Splash_10098.jpg into: nsstring:Splash_1 int:0098 nsstring:jpg 10x all!
Upvotes: 0
Views: 199
Reputation: 23278
Using Regex
(NSRegularExpression
in iOS), this can be done very easily,
Check this out,
NSError *error = NULL;
NSString *originalString = @"Splash_10098.jpg";
NSString *regexString = @"([^\?]*_[0-9])([0-9]*)(.)([a-z]*)";
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:originalString options:NSRegularExpressionCaseInsensitive range:NSMakeRange(0, [originalString length])];
NSLog(@"FileName: %@", [originalString substringWithRange:[match rangeAtIndex:1]]);
NSLog(@"Total Count: %@", [originalString substringWithRange:[match rangeAtIndex:2]]);
NSLog(@"File type: %@", [originalString substringWithRange:[match rangeAtIndex:4]]);
Result:
FileName: Splash_1
Total Count: 0098
File type: jpg
Upvotes: 0
Reputation: 4725
If the number length is fixed in the suffix, it would make sense to use a substring instead of trying to remove the prefix. Strip the extension and grab the last x characters, convert those into an int with either intValue
or the NSNumberFormatter
suggested by iOS, although that might be unnecessary if you are sure of the format of the string.
NSString *userProvidedString = @"Splash_10001.jpg";
NSString *numberString = [userProvidedString stringByDeletingPathExtension];
NSUInteger length = [numberString length];
NSInteger numberLength = 4;
if (length < numberLength)
{
NSLog(@"Error in the string");
return;
}
numberString = [numberString substringWithRange: NSMakeRange(length - 4, 4)];
NSInteger integer = [numberString integerValue];
// Do whatever you want with the integer.
Upvotes: 0
Reputation: 813
It depends what input are of the string is granted. In the following I would search for the dot and go backwards to the maximum of digits. By the way I could only recommend to use the multi lingual NumberFormatter instead of relying on the default conversion.
NSString * input = @"Splash_19001.jpg";
NSRange r = [input rangeOfString:@"."];
if(r.location>4){
NSString * numberPart = [input substringWithRange: NSMakeRange(r.location-4,4)];
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * number = [nf numberFromString:numberPart];
int val = [number intValue];
NSLog(@"intValue=%d",val);
}
Upvotes: 1
Reputation: 1001
I think this is what you're looking for
NSString *stringUserSendsYou = @"Splash_10098.jpg";
int IMAGE_COUNT = [[[stringUserSendsYou stringByReplacingOccurrencesOfString:@"Splash_1" withString:@""] stringByReplacingOccurrencesOfString:@".jpg" withString:@""] integerValue];
Upvotes: 0