Reputation: 1845
I am working with array of images containing images with jpg,png and gif extensions.I need to append a string @"-Add" to every image in array just before the extension.How can i achieve this. For ex :i have a image DSC004.jpg and i want to append string "-Add" ,so that my image name becomes DSC004-Add.jpg.?????
I thought of deleting the extension ,appending the required string and then again appending the extension.But my array of images has different extension with every image name.PLs help me out
Upvotes: 3
Views: 4484
Reputation: 93
you can go with this one also:
NSError *error= NULL;
NSString *myString = @"DSC004.jpg";
NSMutableString *tempStr =[[NSMutableString alloc] initWithString:myString];
NSRegularExpression *regex =[NSRegularExpression regularExpressionWithPattern:@"(\\..{3})$" options:NSMatchingReportCompletion error:&error];
[regex replaceMatchesInString:tempStr options:NSMatchingReportCompletion range:NSMakeRange(0,[myString length]) withTemplate:@"-Add$0"];
NSLog(@"%@",tempStr);
Here regex engine looks for the pattern <dot> followed by three characters
and save it in $0
then replace it with -Add followed by saved value(match) in $0
Hope it helps too!
Upvotes: 0
Reputation: 794
NSString *str = @"ajdgl.jpg";
NSArray *array = [str componentsSeparatedByString:@"."];
NSString *newStr;
for (int i=0; i<[array count]-1; i++) {
newStr = [NSString stringWithFormat:@"%@.%@",newStr,[array objectAtIndex:i]];
}
NSString *UpdatedName = [NSString stringWithFormat:@"%@-add.%@",newStr,[array objectAtIndex:[array count]-1]];
Here first it will concat all the previous string and dot(.) also.. This will with any number of dots. in between your image name..
Upvotes: 2
Reputation: 107231
You can use the following code if your file name is like some.jpg
:
NSString *newString = [@"DSC004.jpg" stringByReplacingOccurrencesOfString:@"." withString:@"-Add."];
If it is like some.some.jpg
use:
NSArray *arrayString = [fileName componentsSeparatedByString:@"."];
NSMutableArray *mutableArray = [NSMutableArray arrayWithArray: arrayString];
int arrayCount = [arrayString count];
NSString *newStr = nil;
if(arrayCount>=2)
{
NSString *tempStr = [arrayString objectAtIndex:arrayCount-2];
tempStr = [tempStr stringByAppendingString:@"-Add"];
[mutableArray replaceObjectAtIndex:arrayCount-2 withObject:tempStr];
newStr = [mutableArray componentsJoinedByString:@"."];
NSLog(@"%@",newStr);
}
else
{
newStr = [fileName stringByAppendingString:@"-Add"];
}
Upvotes: 0
Reputation: 1415
NSString *str = @"ajdgl.jpg";
NSArray *arr = [str componentsSeparatedByString:@"."];
NSString *newStr = [NSString stringWithFormat:@"%@-add%@",[arr objectAtIndex:0],[arr objectAtIndex:1]];
Upvotes: 2
Reputation: 318934
The most complete and general way, but not the simplest, is something like this:
NSString *filename = @"DSC004.jpg";
NSString *ext = [filename pathExtension];
NSString *basename = [filename stringByDeletingPathExtension];
NSString *updated = [basename stringByAppendingString:@"-Add"];
NSString *finalName = [updated stringByAppendingPathExtension:ext];
This approach works with full pathnames or filenames and files with multiple periods in the name,.
Upvotes: 15
Reputation: 3260
NSString *original=@"DSC004.jpg";
NSString *target=[original stringByReplacingOccurrencesOfString:@"'.'" withString:@"-Add."];
try this and let me know my friend..
Happy Coding!!1
Upvotes: 0
Reputation: 6427
NSString *newString = [@"DSC004.jpg" stringByReplacingOccurrencesOfString:@"." withString:@"-Add."];
Upvotes: 1