PageRenderTime 352ms CodeModel.GetById 343ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/NSString+Extras.m

http://github.com/bububa/MongoHub-Mac
Objective C | 84 lines | 56 code | 21 blank | 7 comment | 2 complexity | db33ec6ccdf74e2b06f78986502bcf81 MD5 | raw file
 1//
 2//  NSString+Extras.m
 3//  MongoHub
 4//
 5//  Created by Syd on 10-4-28.
 6//  Copyright 2010 ThePeppersStudio.COM. All rights reserved.
 7//
 8
 9#import "NSString+Extras.h"
10
11
12@implementation NSString (Extras)
13
14
15+ (NSString*)stringFromResource:(NSString*)resourceName {
16    NSString *path = [[NSBundle mainBundle] pathForResource:resourceName ofType:nil];
17    return [NSString stringWithContentsOfFile:path usedEncoding:nil error:nil];
18}
19
20# pragma Comparing
21
22- (BOOL)startsWithString:(NSString*)otherString {
23    return [self rangeOfString:otherString].location == 0;
24}
25
26- (BOOL)endsWithString:(NSString*)otherString {
27    return [self rangeOfString:otherString].location == [self length]-[otherString length];
28}
29
30- (BOOL)isPresent {
31    return ![self isEqualToString:@""];
32}
33
34- (NSComparisonResult)compareCaseInsensitive:(NSString*)other {
35    NSString *selfString = [self lowercaseString];
36    NSString *otherString = [other lowercaseString];
37    return [selfString compare:otherString];
38}
39
40- (NSString*)stringByPercentEscapingCharacters:(NSString*)characters {
41    return [(NSString*)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)characters, kCFStringEncodingUTF8) autorelease];
42}
43
44- (NSString*)stringByEscapingURL {
45    return [self stringByPercentEscapingCharacters:@";/?:@&=+$,"];    
46}
47
48- (NSString*)stringByUnescapingURL {
49    return [(NSString*)CFURLCreateStringByReplacingPercentEscapes(NULL, (CFStringRef)self, CFSTR("")) autorelease];
50}
51
52- (BOOL)containsString:(NSString *)aString {
53    return [self containsString:aString ignoringCase:NO];
54}
55
56- (BOOL)containsString:(NSString *)aString ignoringCase:(BOOL)flag {
57    unsigned mask = (flag ? NSCaseInsensitiveSearch : 0);
58    return [self rangeOfString:aString options:mask].length > 0;
59}
60
61- (int)countSubstring:(NSString *)aString ignoringCase:(BOOL)flag {
62    unsigned mask = (flag ? NSCaseInsensitiveSearch : 0);
63    return [self rangeOfString:aString options:mask].length;
64}
65
66- (NSString *)stringByTrimmingWhitespace
67{
68    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
69}
70
71+ (NSNull *)nullValue
72{
73    return [NSNull null];
74}
75
76+ (NSString*)UUIDString {
77    CFUUIDRef theUUID = CFUUIDCreate(NULL);
78    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
79    CFRelease(theUUID);
80    return [(NSString *)string autorelease];
81}
82
83
84@end