-
Notifications
You must be signed in to change notification settings - Fork 43
/
NSString+Additions.h.m
executable file
·134 lines (110 loc) · 3.42 KB
/
NSString+Additions.h.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
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
//
// StringHelper.m
//
//
// Created by Sergio on 22/10/10.
// Copyright 2010 Sergio. All rights reserved.
//
#import "NSString+Additions.h"
@implementation NSString (helper)
- (NSString*) substringFrom: (NSInteger) a to: (NSInteger) b {
NSRange r;
r.location = a;
r.length = b - a;
return [self substringWithRange:r];
}
- (NSInteger) indexOf: (NSString*) substring from: (NSInteger) starts {
NSRange r;
r.location = starts;
r.length = [self length] - r.location;
NSRange index = [self rangeOfString:substring options:NSLiteralSearch range:r];
if (index.location == NSNotFound) {
return -1;
}
return index.location + index.length;
}
- (NSString*) trim {
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (BOOL) startsWith:(NSString*) s {
if([self length] < [s length]) return NO;
return [s isEqualToString:[self substringFrom:0 to:[s length]]];
}
- (BOOL)containsString:(NSString *)aString
{
NSRange range = [[self lowercaseString] rangeOfString:[aString lowercaseString]];
return range.location != NSNotFound;
}
- (NSString *)urlEncodeCopy
{
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef) self,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 );
NSString *str = encodedString;
[encodedString release];
return str;
}
- (NSString *)reverseGeocode
{
NSString *urlEncode = [[self urlEncodeCopy] retain];
NSString *gUrl = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&hl=%@&oe=UTF8", urlEncode, NSLocalizedString(@"language", @"")];
[urlEncode release];
NSString *infoData = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:gUrl]
encoding:NSUTF8StringEncoding
error:nil] autorelease];
NSString *value = @"";
if ((infoData == nil) ||
([infoData isEqualToString:@"[]"]))
{
return value;
}
else
{
NSDictionary *dict = [infoData JSONValue];
NSArray* placemarks = [dict objectForKey:@"Placemark"];
for (NSDictionary* placemark in placemarks)
{
NSDictionary* point = [placemark objectForKey:@"Point"];
NSArray* coordinates = [point objectForKey:@"coordinates"];
value = [NSString stringWithFormat:@"%.10f,%.10f", [coordinates objectAtIndex:0], [coordinates objectAtIndex:1]];
break;
}
}
return value;
}
- (NSString *)shortURL
{
NSString *apiEndpoint = [NSString stringWithFormat:@"http://is.gd/api.php?longurl=%@", self];
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
encoding:NSASCIIStringEncoding
error:nil];
return shortURL;
}
- (NSString *)reformatTelephone
{
if ([self containsString:@"-"])
{
self = [self stringByReplacingOccurrencesOfString:@"-" withString:@""];
}
if ([self containsString:@" "])
{
self = [self stringByReplacingOccurrencesOfString:@" " withString:@""];
}
if ([self containsString:@"("])
{
self = [self stringByReplacingOccurrencesOfString:@"(" withString:@""];
}
if ([self containsString:@")"])
{
self = [self stringByReplacingOccurrencesOfString:@")" withString:@""];
}
return self;
}
- (BOOL)containsNullString
{
return ([[self lowercaseString] containsString:@"null"]) ? YES : NO;
}
@end