5 // Copyright (c) 2005 A. Karl Keller (http://karlk.net)
7 // This code is open-source, free software, made available without warranty under
8 // the terms of the GNU General Public License, either version 2 or later (see
9 // http://www.gnu.org/licenses/gpl.html or included copy); as such, it may be
10 // redistributed and/or modified in accordance with that document.
13 #import "RTKStringCategory.h"
16 @implementation NSString (RTKStringCategory)
20 NSMutableString * string = [self mutableCopy];
21 [string replaceOccurrencesOfString:@":"
23 options:NSLiteralSearch
24 range:NSMakeRange(0,[string length])];
25 return [string pathExtension];
30 NSMutableString * string = [self mutableCopy];
31 [string replaceOccurrencesOfString:@":"
33 options:NSLiteralSearch
34 range:NSMakeRange(0,[string length])];
35 string = [[string stringByDeletingPathExtension] mutableCopy];
36 return [[string componentsSeparatedByString:@" "] lastObject];
41 NSMutableArray * stringArray = [[self componentsSeparatedByString:@" "] mutableCopy];
42 [stringArray removeLastObject];
43 return [stringArray componentsJoinedByString:@" "];
48 // These three bytes go at the front of every utf8 text file.
54 NSData * data = [self dataUsingEncoding:NSUTF8StringEncoding];
57 NSLog(@"nil data from string in utf8Data of RTKStringCategory");
59 // Get extra room for the utf8 marker.
60 void * outputData = malloc([data length] + 3);
61 void * rawData = (void *) [data bytes];
63 // Copy marker and text together.
64 memcpy(outputData, &utf8Marker, 3);
65 memcpy(outputData + 3, rawData, [data length]);
67 // And we are done exporting plain text!
68 data = [NSData dataWithBytesNoCopy:outputData
69 length:[data length] + 3
74 - (BOOL)containsSubstring:(NSString *)substring
76 return ([self rangeOfString:substring].location != NSNotFound);
79 - (BOOL)containsCaseInsensitiveSubstring:(NSString *)substring
81 return [[self lowercaseString] containsSubstring:[substring lowercaseString]];