我需要知道文件名exp中我的图像有多么简洁: 我有图像电话: 第一档: Splash_10001.jpg 最后一档: Splash_10098.jpg 我想插入阵列..
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];
}
我想用数字98替换IMAGE_COUNT但是我需要从用户发送给我的字符串中获取numbre:Splash_10098.jpg 我需要将Splash_10098.jpg分成:nsstring:Splash_1 int:0098 nsstring:jpg 10倍全部!
答案 0 :(得分:1)
这取决于授予字符串的输入。在下面我将搜索点并向后移动到最大数字。 顺便说一句,我只建议使用多语言NumberFormatter而不是依赖于默认转换。
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);
}
答案 1 :(得分:0)
我认为这就是你要找的东西
NSString *stringUserSendsYou = @"Splash_10098.jpg";
int IMAGE_COUNT = [[[stringUserSendsYou stringByReplacingOccurrencesOfString:@"Splash_1" withString:@""] stringByReplacingOccurrencesOfString:@".jpg" withString:@""] integerValue];
答案 2 :(得分:0)
如果数字长度在后缀中固定,则使用子字符串而不是尝试删除前缀是有意义的。剥离扩展并抓取最后x个字符,将它们转换为带有intValue
或iOS建议的NSNumberFormatter
的int,但如果您确定字符串的格式,则可能没有必要。 / p>
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.
答案 3 :(得分:0)
使用Regex
(iOS中的NSRegularExpression
),这可以非常轻松地完成,
检查一下,
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]]);
<强>结果:强>
FileName: Splash_1
Total Count: 0098
File type: jpg