3

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

1
  • 1
    hello Sujay..if you are sure that your images file contain only one '.' then use any other solution that are suggested by many but if your images name may have more than one '.' then use rmady's solution. Commented Dec 12, 2012 at 6:08

7 Answers 7

15

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,.

Sign up to request clarification or add additional context in comments.

Comments

2
NSString *str = @"ajdgl.jpg";
NSArray *arr = [str componentsSeparatedByString:@"."];
NSString *newStr  = [NSString stringWithFormat:@"%@-add%@",[arr objectAtIndex:0],[arr objectAtIndex:1]];

1 Comment

This code will crash if the name has no period at all. This gives the wrong result if there is more than one period.
2
  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..

Comments

1
NSString *newString = [@"DSC004.jpg" stringByReplacingOccurrencesOfString:@"." withString:@"-Add."];

Comments

0
NSString *original=@"DSC004.jpg";

NSString *target=[original stringByReplacingOccurrencesOfString:@"'.'" withString:@"-Add."];

try this and let me know my friend..

Happy Coding!!1

Comments

0

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"];
}

4 Comments

You don't use mutableArray. You reference the non-existent tempArray. You don't handle the case of the filename having no extension. You call replaceObjectAtIndex:withObject: on arrayString which isn't mutable. This code won't compile and if it did, it would crash.
@rmaddy: thanks for notifying. I fixed it. If there is no extension it won't enter to the if condition
If there is no extension this leaves newStr as nil. That's not good.
OK, this looks like it will work now. But in all honesty, which code would you rather use for this task - yours or mine? :)
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.