NSString+Wrapper.m
6.71 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// NSString+Wrapper.m
// WiFiHelpers
//
// Created by yy on 14-2-11.
// Copyright (c) 2014年 yy. All rights reserved.
//
#import "NSString+Wrapper.h"
#import <CommonCrypto/CommonDigest.h>
@implementation NSString (Wrapper)
#define JavaNotFound -1
/** Java-like method. Returns the char value at the specified index. */
- (unichar) charAt:(int)index {
return [self characterAtIndex:index];
}
/**
* Java-like method. Compares two strings lexicographically.
* the value 0 if the argument string is equal to this string;
* a value less than 0 if this string is lexicographically less than the string argument;
* and a value greater than 0 if this string is lexicographically greater than the string argument.
*/
- (int) compareTo:(NSString*) anotherString {
return [self compare:anotherString];
}
/** Java-like method. Compares two strings lexicographically, ignoring case differences. */
- (int) compareToIgnoreCase:(NSString*) str {
return [self compare:str options:NSCaseInsensitiveSearch];
}
/** Java-like method. Returns true if and only if this string contains the specified sequence of char values. */
- (BOOL) contains:(NSString*) str {
NSRange range = [self rangeOfString:str];
return (range.location != NSNotFound);
}
- (BOOL) startsWith:(NSString*)prefix {
return [self hasPrefix:prefix];
}
- (BOOL) endsWith:(NSString*)suffix {
return [self hasSuffix:suffix];
}
- (BOOL) equals:(NSString*) anotherString {
return [self isEqualToString:anotherString];
}
- (BOOL) equalsIgnoreCase:(NSString*) anotherString {
return [[self toLowerCase] equals:[anotherString toLowerCase]];
}
- (int) indexOfChar:(unichar)ch{
return [self indexOfChar:ch fromIndex:0];
}
- (int) indexOfChar:(unichar)ch fromIndex:(int)index{
int len = (int)self.length;
for (int i = index; i < len; ++i) {
if (ch == [self charAt:i]) {
return i;
}
}
return JavaNotFound;
}
- (int) indexOfString:(NSString*)str {
NSRange range = [self rangeOfString:str];
if (range.location == NSNotFound) {
return JavaNotFound;
}
return (int)range.location;
}
- (int) indexOfString:(NSString*)str fromIndex:(int)index {
NSRange fromRange = NSMakeRange(index, self.length - index);
NSRange range = [self rangeOfString:str options:NSLiteralSearch range:fromRange];
if (range.location == NSNotFound) {
return JavaNotFound;
}
return (int)range.location;
}
- (int) lastIndexOfChar:(unichar)ch {
int len = (int)self.length;
for (int i = len-1; i >=0; --i) {
if ([self charAt:i] == ch) {
return i;
}
}
return JavaNotFound;
}
- (int) lastIndexOfChar:(unichar)ch fromIndex:(int)index {
int len = (int)self.length;
if (index >= len) {
index = len - 1;
}
for (int i = index; i >= 0; --i) {
if ([self charAt:i] == ch) {
return index;
}
}
return JavaNotFound;
}
- (int) lastIndexOfString:(NSString*)str {
NSRange range = [self rangeOfString:str options:NSBackwardsSearch];
if (range.location == NSNotFound) {
return JavaNotFound;
}
return (int)range.location;
}
- (int) lastIndexOfString:(NSString*)str fromIndex:(int)index {
NSRange fromRange = NSMakeRange(0, index);
NSRange range = [self rangeOfString:str options:NSBackwardsSearch range:fromRange];
if (range.location == NSNotFound) {
return JavaNotFound;
}
return (int)range.location;
}
- (NSString *) substringFromIndex:(int)beginIndex toIndex:(int)endIndex {
if (endIndex <= beginIndex) {
return @"";
}
NSRange range = NSMakeRange(beginIndex, endIndex - beginIndex);
return [self substringWithRange:range];
}
- (NSString *) toLowerCase {
return [self lowercaseString];
}
- (NSString *) toUpperCase {
return [self uppercaseString];
}
- (NSString *) trim {
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (NSString *) replaceAll:(NSString*)origin with:(NSString*)replacement {
return [self stringByReplacingOccurrencesOfString:origin withString:replacement];
}
- (NSArray *) split:(NSString*) separator {
return [self componentsSeparatedByString:separator];
}
+ (NSString*) uniqueString
{
CFUUIDRef uuidObj = CFUUIDCreate(nil);
NSString *uuidString = (__bridge_transfer NSString*)CFUUIDCreateString(nil, uuidObj);
CFRelease(uuidObj);
return uuidString;
}
- (NSString*) urlEncodedString {
CFStringRef encodedCFString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(__bridge CFStringRef) self,
nil,
CFSTR("?!@#$^&%*+,:;='\"`<>()[]{}/\\| "),
kCFStringEncodingUTF8);
NSString *encodedString = [[NSString alloc] initWithString:(__bridge_transfer NSString*) encodedCFString];
if(!encodedString)
encodedString = @"";
return encodedString;
}
- (NSString*) urlDecodedString {
CFStringRef decodedCFString = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
(__bridge CFStringRef) self,
CFSTR(""),
kCFStringEncodingUTF8);
// We need to replace "+" with " " because the CF method above doesn't do it
NSString *decodedString = [[NSString alloc] initWithString:(__bridge_transfer NSString*) decodedCFString];
return (!decodedString) ? @"" : [decodedString stringByReplacingOccurrencesOfString:@"+" withString:@" "];
}
+ (NSString *)transformToString:(id)obj
{
NSString *key = nil;
if ( [obj isKindOfClass:[NSNumber class]] )
{
key = [(NSNumber *)obj stringValue];
}
else if ( [obj isKindOfClass:[NSString class]] )
{
key = (NSString *)obj;
}
else
{
return @"";
}
return key;
}
+(NSNumber*)transformToNumber:(id)obj
{
NSNumber *key = nil;
if ( [obj isKindOfClass:[NSNumber class]] )
{
key = (NSNumber *)obj;
}
else if ( [obj isKindOfClass:[NSString class]] )
{
key = [NSNumber numberWithLongLong:[(NSString *)obj longLongValue]];
}
else
{
return [NSNumber numberWithInteger:NSNotFound];
}
return key;
}
+ (NSString *)stringWithInt:(NSInteger)value {
return [NSString stringWithFormat:@"%ld", (long)value];
}
@end