IOS学习笔记十六(NSString和NSMutableString)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                IOS学习笔记十六(NSString和NSMutableString)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                1、NSString和NSMutableString
NSString是不變字符串類,有點像java里面的String,NSMutableString是可變字符串類,有點類似java里面的StringBuffer
?
 2、測試demo
 
int main(int argc, char * argv[]) {@autoreleasepool {unichar data[6] = {97, 98, 100, 101, 102, 103};NSString *str = [[NSString alloc] initWithCharacters:data length:6];NSLog(@"str is %@", str);char *cstr = "chenyu";NSString *str2 = [NSString stringWithUTF8String:cstr];NSLog(@"str2 is %@", str2);NSString *str3 = @"chenyu";NSString *name = @"陳喻";str3 = [str3 stringByAppendingString:@"chenyu"];NSLog(@"str3 is %@", str3);const char *cstr1 = [str3 UTF8String];NSLog(@"cstr1 is %s", cstr1);str3 = [str3 stringByAppendingFormat:@"hello %@ hello", name];NSLog(@"str3 is %@", str3);NSLog(@"str3 length is %lu", [str3 length]);NSString *s1 = [str3 substringToIndex:10];NSLog(@"s1 is %@", s1);NSString *s2 = [str3 substringFromIndex:5];NSLog(@"s2 is %@", s2);NSString *s3 = [str3 substringWithRange:NSMakeRange(5,10)];NSLog(@"s3 is %@", s3);NSRange pos = [str3 rangeOfString:@"陳喻"];NSLog(@"陳喻在str3中開始的位置:%ld,長度為%ld", pos.location, pos.length);str3 = [str3 uppercaseString];NSLog(@"str3 is %@", str3);NSMutableString *tstr = [NSMutableString stringWithString:@"hello"];[tstr appendString:@"chenyu"];NSLog(@"tstr is %@", tstr);[tstr appendFormat:@"hello word %@", @"chengongyu"];NSLog(@"tstr is %@", tstr);[tstr insertString:@"hello" atIndex:6];NSLog(@"tstr is %@", tstr);[tstr deleteCharactersInRange:NSMakeRange(6, 9)];NSLog(@"tstr is %@", tstr);[tstr replaceCharactersInRange:NSMakeRange(3, 6) withString:@"objectobject"];NSLog(@"tstr is %@", tstr);}
} 
?
?
?
?
?
?
3、運行結果
str is abdefg str2 is chenyu str3 is chenyuchenyu cstr1 is chenyuchenyu str3 is chenyuchenyuhello 陳喻 hello str3 length is 26 s1 is chenyuchen s2 is uchenyuhello 陳喻 hello s3 is uchenyuhel 陳喻在str3中開始的位置:18,長度為2 str3 is CHENYUCHENYUHELLO 陳喻 HELLO tstr is hellochenyu tstr is hellochenyuhello word chengongyu tstr is hellochellohenyuhello word chengongyu tstr is hellocuhello word chengongyu tstr is helobjectobjectllo word chengongyu?
?
?
?
總結
以上是生活随笔為你收集整理的IOS学习笔记十六(NSString和NSMutableString)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Android Studio之导入安卓项
- 下一篇: linux平台之如何查看svn账号
