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
| #pragma mark - 2、字符串的常用功能 -(void)commonFunction{ NSString * str = @"Hello"; NSString * book = @",book"; NSString * strBook = [str stringByAppendingString:book]; NSLog(@"str = %@",str); NSLog(@"strBook = %@",strBook); NSString * fStr = @"Hello"; NSString * formatStr = [fStr stringByAppendingFormat:@",%@Book",@"Good"]; NSLog(@"fStr = %@",fStr); NSLog(@"formatStr = %@",formatStr); NSString * string = @"hello,this is a good book"; NSString * subStr1 = [string substringToIndex:10]; NSString * subStr2 = [string substringFromIndex:10]; NSString * subStr3 = [string substringWithRange:NSMakeRange(2, 3)]; NSLog(@"subStr1 = %@",subStr1); NSLog(@"subStr2 = %@",subStr2); NSLog(@"subStr3 = %@",subStr3); NSString * mainString = @"hello,world"; NSString * subStr = @"world"; NSRange range = [mainString rangeOfString:subStr]; NSLog(@"range.location = %ld,range,length = %ld",range.location,range.length); NSString * smallBigStr = @"Hello,World"; NSString * smallStr = [smallBigStr lowercaseString]; NSString * bigStr = [smallBigStr uppercaseString]; NSLog(@"smallStr = %@",smallStr); NSLog(@"bigStr = %@",bigStr); NSString * preString = @"hello,world"; BOOL hasPre = [preString hasPrefix:@"hello"]; NSLog(@"hasPre = %d",hasPre); NSString * sufString = @"hello,world"; [sufString hasSuffix:@"world"]; NSLog(@"sufString = %d",hasPre); NSString * lengthStr = @"hello,world"; NSLog(@"lengthStr.length = %ld",lengthStr.length); NSString * charStr = @"hello,world"; unichar c = [charStr characterAtIndex:0]; NSLog(@"c = %c",c); NSString * compareStr1 = @"hello"; NSString * compareStr2 = @"world"; NSComparisonResult result = [compareStr1 compare:compareStr2]; NSLog(@"result = %d",result); BOOL isEqual = [compareStr1 isEqualToString:compareStr2]; NSLog(@"isEqual = %d",isEqual); NSString * sepStr = @"hello-world"; NSArray * setArray = [sepStr componentsSeparatedByString:@"-"]; NSLog(@"setArray = %@",setArray); NSCharacterSet * charSet = [NSCharacterSet characterSetWithCharactersInString:@"-o"]; NSArray * array = [sepStr componentsSeparatedByCharactersInSet:charSet]; NSLog(@"array = %@",array); }
#pragma mark - 3、可变字符串 -(void)mutableStringFun{ NSMutableString * mString = [[NSMutableString alloc] initWithString:@"hello"]; [mString appendString:@",book"]; NSLog(@"mString = %@",mString); [mString appendFormat:@".%@ Good Book",@"This Is A"]; NSLog(@"mString = %@",mString); [mString deleteCharactersInRange:NSMakeRange(0, 6)]; NSLog(@"mString = %@",mString); [mString replaceCharactersInRange:NSMakeRange(0, 6) withString:@"你好"]; NSLog(@"mString = %@",mString); [mString insertString:@"你好" atIndex:7]; NSLog(@"mString = %@",mString); [mString setString:@"你好,世界"]; NSLog(@"mString = %@",mString); }
|