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