NSString *content = @"<phone>12345678963</phone><address>78945612333</address>"; NSRange str = [content rangeOfString:@"<address>"]; //截取固定部分 NSString *substr = [content substringWithRange:str]; NSLog(@"address is %@",substr); // 拼接文件夾用 / NSString *try1 = [substr stringByAppendingPathComponent:content]; NSLog(@"try 1 = %@",try1); // 拼接文件夾用 . NSString *try2 = [content stringByAppendingPathExtension:substr]; NSLog(@"try 2 = %@",try2); // 截取從選定位置開(kāi)始 并指定長(zhǎng)度 NSString *try3 = [content substringWithRange:NSMakeRange(str.location, 20)]; NSLog(@"try3 = %@",try3); NSArray *try4 = [content componentsSeparatedByString:@">"]; NSLog(@"try4 = %@",try4); // 從指定位置開(kāi)始截取 NSRange range1 = NSMakeRange(str.location, str.length); NSString *try5 = [content substringFromIndex:range1.location +range1.length]; NSLog(@"try5 = %@",try5); //截取到指定位置 NSRange str1 = [try5 rangeOfString:@"</address>"]; NSRange range2 = NSMakeRange(str1.location, str1.length); NSString *try6 = [try5 substringToIndex:range2.location]; NSLog(@"try6 = %@",try6); //設(shè)置字符串首字母大寫(xiě) NSLog(@"str0首字母大寫(xiě):%@",[str0 capitalizedString]); //設(shè)置字符串全部?jī)?nèi)容為大寫(xiě) NSLog(@"str0大寫(xiě):%@",[str0 uppercaseString]); //設(shè)置字符全部?jī)?nèi)容為小寫(xiě) NSLog(@"str0小寫(xiě):%@",[str0 lowercaseString]); //搜索查找 NSRange range = [str rangeOfString:@"345"]; if (range.location == NSNotFound) { //沒(méi)有找到 } //截取 從第3個(gè)開(kāi)始到之后的 [str substringFromIndex:3]; 截取到第5個(gè) [str substringToIndex:5]; 從第2個(gè)開(kāi)始 截取4個(gè) [str substringWithRange:NSMakeRange(2, 4)]; // 關(guān)于大小寫(xiě)的轉(zhuǎn)化 //字符串轉(zhuǎn)換 NSString *str=@"abc"; NSString *str=@"ABC"; NSLog(@"uppercase:%@",[str uppercaseString]);//打印出ABC NSLog(@"lowercase:%@",[stri lowercaseString]);//打印出abc 字符串轉(zhuǎn)化 NSString *str = @"100"; //轉(zhuǎn)int int a = [str intValue]; //算字?jǐn)?shù) int len = [str length]; //取字符 unichar c = [str characterAtIndex:0]; //返回c語(yǔ)言的字符串 char *s = [str UTF8String]; 除了NSString,還有一個(gè)NSMutableString是可變字符串,,是NSString的子類,。 NSMutableString *str = [[NSMutableString alloc] initWithCapacity:8]; [str setString:@"1234"]; 拼接 [str appendString:@"567890"]; 替換 [str replaceCharactersInRange:[str rangeOfString:@"456"] withString:@"xxx"]; 插入 [str insertString:@"yyy" atIndex:6]; 刪除 [str deleteCharactersInRange:[str rangeOfString:@"xxyy"]]; NSLog(@"%@", str); |
|
來(lái)自: 玄冰優(yōu) > 《iOS 點(diǎn)滴》