标签:
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) { 4 @autoreleasepool { 8 /* 9 不可变字符串 10 1.创建字符串 11 实例方法(3种) 12 类方法(3种) 13 2.不可变字符串: 14 统计长度(2种) 15 索引提取 (3种) 16 比较 (3种) 17 查照字符串位置(1种) 18 判断前后缀 19 转换数字 20 大小写转换 21 字符串提取 22 23 可变字符串 24 1.创建可变字符串 25 2.可变字符串的操作 26 增(3种)删(1种)改(1种)查 27 28 29 */ 30 31 //实例方法(3种) 32 NSString *str1 = @"123456789"; 33 NSString *str2 = [[NSString alloc] initWithFormat:@"%d4567",3]; 34 // NSString *str3 = [[NSString alloc] initWithString:str2];//传的是对象 35 36 //类方法(3种) 37 NSString *str4 = [NSString stringWithFormat:@"Abc%@eFg",@"d"]; 38 // NSString *str5 = [NSString stringWithString:str4]; 39 40 41 42 //统计长度(2种) 43 NSUInteger length = [str1 length]; 44 NSUInteger length1 = str1.length; 45 NSLog(@"%lu %lu",length,length1); 46 47 48 //索引提取 (3种) 49 NSString *newStr1 = [str1 substringFromIndex:3]; 50 NSLog(@"%@",newStr1); 51 NSString *newStr2 = [str1 substringToIndex:3]; 52 NSLog(@"%@",newStr2); 53 NSString *newStr3 = [str1 substringWithRange:NSMakeRange(3, 4)]; 54 NSLog(@"%@",newStr3); 55 56 57 //比较 (3种) 58 if ([str1 isEqualToString:str2]) { 59 NSLog(@"相同"); 60 }else 61 NSLog(@"不相同"); 62 63 64 65 if ([str1 compare:str2] == NSOrderedAscending) { 66 NSLog(@"str1>str2"); 67 } 68 else if ([str1 compare:str2] == NSOrderedDescending) 69 NSLog(@"str1<str2"); 70 else if ([str1 compare:str2] == NSOrderedSame) 71 NSLog(@"=="); 72 73 74 75 if ([str1 caseInsensitiveCompare:str2] == NSOrderedAscending) { 76 NSLog(@"str1>str2"); 77 } 78 else if ([str1 caseInsensitiveCompare:str2] == NSOrderedDescending) 79 NSLog(@"str1<str2"); 80 else if ([str1 caseInsensitiveCompare:str2] ==NSOrderedSame) 81 NSLog(@"=="); 82 83 84 // 查找字符串位置 85 NSRange range = [str1 rangeOfString:str2]; 86 if (range.location != NSNotFound) { 87 NSLog(@"location:%lu length:%lu",range.location,range.length); 88 89 } else { 90 NSLog(@"没找到"); 91 } 92 93 94 //判断前后缀(2种) 95 BOOL flag = [str4 hasPrefix:@"abc"]; 96 NSLog(@"%d",flag); 97 98 BOOL anotherFlag = [str4 hasSuffix:@"fg"]; 99 NSLog(@"%d",anotherFlag); 100 101 102 //转换数字(4种) 103 int number = [str1 intValue]; 104 NSLog(@"%d",number); 105 106 NSInteger number1 = [str1 intValue]; 107 NSLog(@"%ld",number1); 108 109 NSString *str6 = @"1234567.89"; 110 double number2 = [str6 doubleValue]; 111 NSLog(@"%lf",number2); 112 113 //大小写转换 114 NSString *upStr = [str4 uppercaseString]; 115 NSLog(@"%@",upStr); 116 NSString *lowStr = [str4 lowercaseString]; 117 NSLog(@"%@",lowStr); 118 NSString *tempStr = [str4 capitalizedString]; 119 NSLog(@"%@",tempStr); 120 121 122 123 124 // 可变字符串 125 // 实例方法 126 NSMutableString *muStr1 = [[NSMutableString alloc] 127 initWithFormat:@"1%d3",2]; 128 129 NSMutableString *muStr2 = [[NSMutableString alloc] 130 initWithString:@"123"]; 131 132 NSMutableString *muStr3 = [[NSMutableString alloc] 133 initWithCapacity:10]; 134 135 // 类方法 136 NSMutableString *muStr4 = [NSMutableString stringWithFormat:@"4%d6",5]; 137 138 NSMutableString *muStr5 = [NSMutableString stringWithString:@"123"]; 139 140 NSMutableString *muStr6 = [NSMutableString stringWithCapacity:10]; 141 142 143 //增(3种) 144 145 [muStr1 appendFormat:@"%d",456]; 146 NSLog(@"%@",muStr1); 147 148 [muStr2 appendString:@"789"]; 149 NSLog(@"%@",muStr2); 150 151 [muStr3 insertString:@"abcdefghijklmn" atIndex:0]; 152 NSLog(@"%@",muStr3); 153 154 [muStr4 insertString:@"abcdefghijklmn" atIndex:2]; 155 NSLog(@"%@",muStr4); 156 157 158 159 //删除 160 [muStr5 deleteCharactersInRange:NSMakeRange(2, 1)]; 161 NSLog(@"%@",muStr5); 162 163 //改 164 [muStr6 replaceCharactersInRange:NSMakeRange(0, 0) withString:@"1234567890"]; 165 NSLog(@"%@",muStr6); 166 167 [muStr5 replaceCharactersInRange:NSMakeRange(0, 1) withString:@"abcd"]; 168 NSLog(@"%@",muStr5);
1 #import <Foundation/Foundation.h> 2 3 @interface MyStringTools : NSObject 4 5 /* 6 7 1.创建一个类,要求: 8 (1)、定义一个方法,能完成字符串逆序输出的操作 9 (2)、定义一个方法,能删除某字符串中的指定字串 10 11 myStringTool delete:(NSString*) InString:(NSString*) 12 13 (3)、定义一个方法,能查找某字符串在某字符串中出现的次数 14 (4)、定义一个方法,能传入两个字符串,将这个字符串组合成一个新的对称字符串返回 15 (5)、定义一个方法,能将原字符串进行小写转大写,大写转小写 16 (6)、定义一个方法,能压缩传入的字符串 17 (比如输入@"aaabbbcceaa" 输出@"3a3b2c1e2a") 18 (7)、定义一个方法,能对传入的字符串按照字母顺序进行排列 19 (比如输入@"aczabczab" 输出@"aaabbcczz”) 20 (8)、定义一个方法,能够替换某字符串中的某个指定字串 21 (9)、定义一个方法,能够获取指定字符串长度。 22 */ 23 24 //定义一个方法,能完成字符串逆序输出的操作 25 +(NSString *) reverseOrder:(NSString *) str1; 26 27 //定义一个方法,能删除某字符串中的指定字串 28 +(NSString *) deleteTheString:(NSString *) str1 29 inString:(NSString *) str2; 30 31 //定义一个方法,能查找某字符串在某字符串中出现的次数 32 +(NSInteger) findNumberOfString:(NSString *) str1 33 inString:(NSString *) str2; 34 35 //定义一个方法,能传入两个字符串,将这个字符串组合成一个新的对称字符串返回 36 +(NSString *) makeUpString:(NSString *) str1 37 andString:(NSString *) str2; 38 39 //定义一个方法,能将原字符串进行小写转大写,大写转小写 40 +(NSString *) convertLetters:(NSString *) str1 andCase:(NSString *) mycase; 41 42 //定义一个方法,能压缩传入的字符串 43 +(NSString *) compressString:(NSString *) str1; 44 45 //定义一个方法,能对传入的字符串按照字母顺序进行排列 46 +(NSString *) theStringOrder:(NSString *) str1; 47 48 //定义一个方法,能够替换某字符串中的某个指定字串 49 +(NSString *) replaceTheString:(NSString *) str1 50 fromString:(NSString *) str2 51 toString:(NSString *) str3; 52 53 //定义一个方法,能够获取指定字符串长度 54 +(NSInteger) obtainTheLength:(NSString *) str1; 55 @end 56 57 58 59 60 61 62 63 #import "MyStringTools.h" 64 65 @implementation MyStringTools 66 67 +(NSString *) reverseOrder:(NSString *) str1 68 { 69 NSMutableString *string = [NSMutableString stringWithString:str1]; 70 NSMutableString *mutableString =[NSMutableString stringWithString:str1]; 71 72 73 for (int i=0; i<string.length; i++) { 74 //方法1 75 NSString *subStr1 = [string substringWithRange:NSMakeRange(i, 1)]; 76 NSString *subStr2 = [string substringWithRange: 77 NSMakeRange(string.length - i-1, 1)]; 78 // 取长度和范围 79 NSRange range1 = [string rangeOfString:subStr1]; 80 NSRange range2 = [string rangeOfString:subStr2]; 81 82 [mutableString replaceCharactersInRange:range1 withString:subStr2]; 83 [mutableString replaceCharactersInRange:range2 84 withString:subStr1]; 85 } 86 return mutableString; 87 88 //方法2 89 // NSString *tempStr = [string substringFromIndex:string.length-1]; 90 // 91 // [string insertString:tempStr atIndex:i]; 92 // 93 // [string deleteCharactersInRange:NSMakeRange(string.length-1,1)]; 94 // } 95 // return string 96 97 } 98 99 +(NSString *) deleteTheString:(NSString *) str1 100 inString:(NSString *) str2 101 { 102 NSMutableString *string = [NSMutableString stringWithString:str2]; 103 104 NSRange range = [string rangeOfString:str1]; 105 106 if (range.location !=NSNotFound) { 107 [string deleteCharactersInRange:range]; 108 } 109 else 110 NSLog(@"没有发现字符"); 111 return string; 112 } 113 114 +(NSInteger) findNumberOfString:(NSString *) str1 115 inString:(NSString *) str2 116 { 117 int i,count=0; 118 119 NSMutableString *string = [NSMutableString stringWithString:str2]; 120 121 for (i=0; i<string.length; i++) { 122 123 NSRange range = [string rangeOfString:str1]; 124 125 if (range.location !=NSNotFound) { 126 [string deleteCharactersInRange:range]; 127 count++; 128 } 129 } 130 return count; 131 } 132 133 134 +(NSString *) makeUpString:(NSString *) str1 135 andString:(NSString *) str2 136 { 137 NSMutableString *string = [NSMutableString stringWithString:str2]; 138 [string appendString:str1]; 139 140 for (NSInteger i=string.length-1; i>=0; i--) { 141 [string appendString:[string substringWithRange:NSMakeRange(i, 1)]]; 142 143 } 144 return string; 145 } 146 147 +(NSString *) convertLetters:(NSString *) str1 andCase:(NSString *) mycase 148 { 149 NSMutableString *string = [NSMutableString stringWithString:str1]; 150 151 if ([mycase isEqualToString:@"大写"]) { 152 return [string uppercaseString]; 153 } 154 else 155 return [string lowercaseString]; 156 } 157 158 +(NSString *) compressString:(NSString *) str1 159 { 160 int i; 161 NSMutableString *string = [NSMutableString stringWithString:str1]; 162 NSMutableString *newString; 163 NSMutableString *newString1 = [NSMutableString stringWithString:@""]; 164 165 for (i=0; i<string.length; i++) { 166 167 NSString *tempStr = [string substringWithRange:NSMakeRange(0, 1)]; 168 int count=0; 169 170 for (int j=0; j<string.length; j++) { 171 172 NSString *tempStr1 = [string substringWithRange:NSMakeRange(0, 1)]; 173 174 if ([tempStr1 isEqualToString:tempStr]) { 175 176 [string deleteCharactersInRange:NSMakeRange(0, 1)]; 177 count++; 178 } 179 } 180 newString = [NSMutableString stringWithFormat:@"%d%@",count,tempStr]; 181 [newString1 appendString:newString]; 182 183 } 184 return newString1; 185 186 } 187 188 +(NSString *) theStringOrder:(NSString *) str1 189 { 190 NSMutableString *string = [NSMutableString stringWithString:str1]; 191 192 for (int i=0; i<string.length-1; i++) { 193 for (int j=0; j<string.length-1; j++) { 194 195 NSString *tempStr1=[string substringWithRange:NSMakeRange(j, 1)]; 196 NSString *tempStr2=[string substringWithRange:NSMakeRange(j+1, 1)]; 197 198 NSRange range1 = [string rangeOfString:tempStr1]; 199 NSRange range2 = [string rangeOfString:tempStr2]; 200 201 if ([tempStr1 compare:tempStr2]==NSOrderedAscending) { 202 203 204 [string replaceCharactersInRange:range2 withString:tempStr1]; 205 [string replaceCharactersInRange:range1 withString:tempStr2]; 206 } 207 } 208 } 209 return string; 210 } 211 212 213 +(NSString *) replaceTheString:(NSString *) str1 214 fromString:(NSString *) str2 215 toString:(NSString *) str3 216 { 217 NSMutableString *string = [NSMutableString stringWithString:str2]; 218 219 NSRange range = [string rangeOfString:str1]; 220 [string replaceCharactersInRange:range withString:str3]; 221 return string; 222 } 223 224 +(NSInteger) obtainTheLength:(NSString *) str1 225 { 226 return str1.length; 227 } 228 229 230 231 232 @end 233 234 235 236 237 238 239 240 #import <Foundation/Foundation.h> 241 #import "MyStringTools.h" 242 243 int main(int argc, const char * argv[]) { 244 @autoreleasepool { 245 246 NSString *mystring=[NSString stringWithFormat:@"abcdef"]; 247 248 NSLog(@"%@",[MyStringTools reverseOrder:mystring]); 249 250 NSLog(@"%@",[MyStringTools deleteTheString:@"bc" 251 inString:mystring]); 252 253 NSLog(@"%ld",[MyStringTools findNumberOfString:@"bc" 254 inString:@"abcdfebcbbc"]); 255 256 NSLog(@"%@",[MyStringTools makeUpString:@"abcdef" 257 andString:@"gh"]); 258 259 NSLog(@"%@",[MyStringTools convertLetters:@"aBcDeF" 260 andCase:@"大写"]); 261 262 NSLog(@"%@",[MyStringTools compressString:@"hhfffkkkkkiinil"]); 263 264 NSLog(@"%@",[MyStringTools theStringOrder:@"sadcbfw"]); 265 266 NSLog(@"%@",[MyStringTools replaceTheString:@"sdf" 267 fromString:@"asdfghjkl" 268 toString:@"abc"]); 269 270 NSLog(@"%lu",[MyStringTools obtainTheLength:@"qwert"]); 271 272 273 274 } 275 return 0; 276 }
标签:
原文地址:http://www.cnblogs.com/oc-bowen/p/5040205.html