|
| 1 | +// |
| 2 | +// NSStringWrapper.m |
| 3 | +// NSStringWrapper |
| 4 | +// |
| 5 | +// Created by Tang Qiao on 12-2-4. |
| 6 | +// Copyright (c) 2012年 Netease. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "NSStringWrapper.h" |
| 10 | + |
| 11 | +@implementation NSString(Wrapper) |
| 12 | + |
| 13 | +#define JavaNotFound -1 |
| 14 | + |
| 15 | +/** Java-like method. Returns the char value at the specified index. */ |
| 16 | +- (unichar) charAt:(int)index { |
| 17 | + return [self characterAtIndex:index]; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Java-like method. Compares two strings lexicographically. |
| 22 | + * the value 0 if the argument string is equal to this string; |
| 23 | + * a value less than 0 if this string is lexicographically less than the string argument; |
| 24 | + * and a value greater than 0 if this string is lexicographically greater than the string argument. |
| 25 | + */ |
| 26 | +- (int) compareTo:(NSString*) anotherString { |
| 27 | + return [self compare:anotherString]; |
| 28 | +} |
| 29 | + |
| 30 | +/** Java-like method. Compares two strings lexicographically, ignoring case differences. */ |
| 31 | +- (int) compareToIgnoreCase:(NSString*) str { |
| 32 | + return [self compare:str options:NSCaseInsensitiveSearch]; |
| 33 | +} |
| 34 | + |
| 35 | +/** Java-like method. Returns true if and only if this string contains the specified sequence of char values. */ |
| 36 | +- (BOOL) contains:(NSString*) str { |
| 37 | + NSRange range = [self rangeOfString:str]; |
| 38 | + return (range.location != NSNotFound); |
| 39 | +} |
| 40 | + |
| 41 | +- (BOOL) startsWith:(NSString*)prefix { |
| 42 | + int len = [prefix length]; |
| 43 | + if (len > self.length) { |
| 44 | + return NO; |
| 45 | + } |
| 46 | + for (int i = 0; i < len; ++i) { |
| 47 | + if ([prefix charAt:i] != [self charAt:i]) { |
| 48 | + return NO; |
| 49 | + } |
| 50 | + } |
| 51 | + return YES; |
| 52 | +} |
| 53 | + |
| 54 | +- (BOOL) endsWith:(NSString*)suffix { |
| 55 | + int len = suffix.length; |
| 56 | + if (len > self.length) { |
| 57 | + return NO; |
| 58 | + } |
| 59 | + int pos = self.length - len; |
| 60 | + for (int i = 0; i < len; ++i, ++pos) { |
| 61 | + if ([self charAt:pos] != [suffix charAt:i]) { |
| 62 | + return NO; |
| 63 | + } |
| 64 | + } |
| 65 | + return YES; |
| 66 | +} |
| 67 | + |
| 68 | +- (BOOL) equals:(NSString*) anotherString { |
| 69 | + int len = [anotherString length]; |
| 70 | + if (len != self.length) { |
| 71 | + return NO; |
| 72 | + } |
| 73 | + for (int i = 0; i < len; ++i) { |
| 74 | + if ([anotherString charAt:i] != [self charAt:i]) { |
| 75 | + return NO; |
| 76 | + } |
| 77 | + } |
| 78 | + return YES; |
| 79 | +} |
| 80 | + |
| 81 | +- (BOOL) equalsIgnoreCase:(NSString*) anotherString { |
| 82 | + return [[self toLowerCase] equals:[anotherString toLowerCase]]; |
| 83 | +} |
| 84 | + |
| 85 | +- (int) indexOfChar:(unichar)ch{ |
| 86 | + return [self indexOfChar:ch fromIndex:0]; |
| 87 | +} |
| 88 | + |
| 89 | +- (int) indexOfChar:(unichar)ch fromIndex:(int)index{ |
| 90 | + int len = self.length; |
| 91 | + for (int i = index; i < len; ++i) { |
| 92 | + if (ch == [self charAt:i]) { |
| 93 | + return i; |
| 94 | + } |
| 95 | + } |
| 96 | + return JavaNotFound; |
| 97 | +} |
| 98 | + |
| 99 | +- (int) indexOfString:(NSString*)str { |
| 100 | + NSRange range = [self rangeOfString:str]; |
| 101 | + if (range.location == NSNotFound) { |
| 102 | + return JavaNotFound; |
| 103 | + } |
| 104 | + return range.location; |
| 105 | +} |
| 106 | + |
| 107 | +- (int) indexOfString:(NSString*)str fromIndex:(int)index { |
| 108 | + NSRange fromRange = NSMakeRange(index, self.length - index); |
| 109 | + NSRange range = [self rangeOfString:str options:NSLiteralSearch range:fromRange]; |
| 110 | + if (range.location == NSNotFound) { |
| 111 | + return JavaNotFound; |
| 112 | + } |
| 113 | + return range.location; |
| 114 | +} |
| 115 | + |
| 116 | +- (int) lastIndexOfChar:(unichar)ch { |
| 117 | + int len = self.length; |
| 118 | + for (int i = len-1; i >=0; --i) { |
| 119 | + if ([self charAt:i] == ch) { |
| 120 | + return i; |
| 121 | + } |
| 122 | + } |
| 123 | + return JavaNotFound; |
| 124 | +} |
| 125 | + |
| 126 | +- (int) lastIndexOfChar:(unichar)ch fromIndex:(int)index { |
| 127 | + int len = self.length; |
| 128 | + if (index >= len) { |
| 129 | + index = len - 1; |
| 130 | + } |
| 131 | + for (int i = index; i >= 0; --i) { |
| 132 | + if ([self charAt:i] == ch) { |
| 133 | + return index; |
| 134 | + } |
| 135 | + } |
| 136 | + return JavaNotFound; |
| 137 | +} |
| 138 | + |
| 139 | +- (int) lastIndexOfString:(NSString*)str { |
| 140 | + NSRange range = [self rangeOfString:str options:NSBackwardsSearch]; |
| 141 | + if (range.location == NSNotFound) { |
| 142 | + return JavaNotFound; |
| 143 | + } |
| 144 | + return range.location; |
| 145 | +} |
| 146 | + |
| 147 | +- (int) lastIndexOfString:(NSString*)str fromIndex:(int)index { |
| 148 | + NSRange fromRange = NSMakeRange(0, index); |
| 149 | + NSRange range = [self rangeOfString:str options:NSBackwardsSearch range:fromRange]; |
| 150 | + if (range.location == NSNotFound) { |
| 151 | + return JavaNotFound; |
| 152 | + } |
| 153 | + return range.location; |
| 154 | +} |
| 155 | + |
| 156 | +- (NSString *) substringFromIndex:(int)beginIndex toIndex:(int)endIndex { |
| 157 | + if (endIndex <= beginIndex) { |
| 158 | + return @""; |
| 159 | + } |
| 160 | + NSRange range = NSMakeRange(beginIndex, endIndex - beginIndex); |
| 161 | + return [self substringWithRange:range]; |
| 162 | +} |
| 163 | + |
| 164 | +- (NSString *) toLowerCase { |
| 165 | + return [self lowercaseString]; |
| 166 | +} |
| 167 | + |
| 168 | +- (NSString *) toUpperCase { |
| 169 | + return [self uppercaseString]; |
| 170 | +} |
| 171 | + |
| 172 | +- (NSString *) trim { |
| 173 | + return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; |
| 174 | +} |
| 175 | + |
| 176 | +- (NSString *) replaceAll:(NSString*)origin with:(NSString*)replacement { |
| 177 | + return [self stringByReplacingOccurrencesOfString:origin withString:replacement]; |
| 178 | +} |
| 179 | + |
| 180 | +- (NSArray *) split:(NSString*) separator { |
| 181 | + return [self componentsSeparatedByString:separator]; |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | +@end |
0 commit comments