[NSString stringByDeletingPathExtension]
Returns a new string made by deleting the extension (if any, and only the last) from the receiver.
- (NSString *)stringByDeletingPathExtension
Return Value
a new string made by deleting the extension (if any, and only the last) from the receiver. Strips any trailing path separator before checking for an extension. If the receiver represents the root path, it is returned unaltered.
Discussion of [NSString stringByDeletingPathExtension]
The following table illustrates the effect of this method on a variety of different paths:
Receiver’s String Value
|
Resulting String
|
---|---|
“/tmp/scratch.tiff ” | “/tmp/scratch ” |
“/tmp/ ” | “/tmp ” |
“scratch.bundle/ ” | “scratch ” |
“scratch..tiff ” | “scratch. ” |
“.tiff ” | “.tiff ” |
“/ ” | “/ ” |
Note that attempting to delete an extension from
@".tiff"
causes the result to be@".tiff"
instead of an empty string. This difference is because a file named@".tiff"
is not considered to have an extension, so nothing is deleted. Note also that this method only works with file paths (not, for example, string representations of URLs).
Example of [NSString stringByDeletingPathExtension]
NSString* theFileName = [[string lastPathComponent] stringByDeletingPathExtension]
Example of [NSString stringByDeletingPathExtension]
@interface NSString (MoreMagic)
- (NSString *)stringByAddingFileSuffix:(NSString *)suffix;
@end
@implementation NSString (MoreMagic)
- (NSString *)stringByAddingFileSuffix:(NSString *)suffix
{
NSString * extension = [self pathExtension];
NSString * baseName = [self stringByDeletingPathExtension];
NSString * thumbBase = [baseName stringByAppendingString:suffix];
return [thumbBase stringByAppendingPathExtension:extension];
}
@end
Example of [NSString stringByDeletingPathExtension]
NSMutableArray *newArray = [[[NSMutableArray alloc] initWithArray:myArray] autorelease];
for( int i = 0; i < [newArray count]; i++ ) {
NSString* oneItem = [newArray objectAtIndex:i];
[newArray replaceObjectAtIndex:i withObject:[oneItem stringByDeletingPathExtension]];
}