1、创建字符串1)NSSring *str = @”adf”;2)NSString *str1 = [NSString new];NSString *str2 = [[NSString alloc] initWithString:@”adf”]; (等同于1)4)NSString *str3 = [NSString stringWithFormat:@”name is %@”,@”小小”];5)char *c = “小小”;NSString *str4 = [[NSString alloc] initWithCString:c encoding:nil];2、获取字符串(截取后创建了新的字符串,不改变原來的字符串)NSString *str = @”www.itcast.cn”;1)获取字符串的长度NSUInteger length = [str length];//str.length2)根据索引获得单个字符unichar c = [str characterAtIndex:5];//索引从0开始3)根据索引获得字符串的一个子串1> 截取字符从某个位置开始到末尾NSString *str1 = [str substingFormIndex:3];//从给定的索引(包含该索引位置)截取到字符串末尾2> 截取字符从某个位置开始到首位NSString *str2 = [str substringToIndex:3];//截取字符串到给定索引的位置(不包含该索引的位置)3> 截取字符从某个位置開始到某个位置結束NSRang *rang = {1,2};//location(索引位置)、length(截取的長度)NSString *str3 = [str substringWithRange:rang];//包含該索引的位置4> 获得一个字符串在字符串中的索引位置、長度NSRange range = [str rangeOfString:str3];NSLog(@”%@”,NSStringFormRange(range));3、判斷字符串是否為空或者相同1)判斷是否是為空Str == nil || str.length == 0;2)判斷是否已指定的內容開頭BOOL isPerfix = [str hasPrefix:@”xww”];3)判斷是否已指定的內容結尾BOOL isSuffix = [str hasSuffix:@”xww”];NSLog(@“%@”,is?@“YES”:@“NO”);4)判斷兩個字符串是否相等NSString *str4 = @“abc”;NSString *str5 = @“abc”;NSString *str6 = [NSString stringWithFormat:@“%@”,@“abc”];1> 用if語句,==來判斷,但是==的意思是內存地址相同,則判斷就相同,str4和str5內存地址相同(運行時回去換存池中找,有相同的就直接拿過來,沒有就創建),和str6的內存地址不相同。2> BOOL isEqual = [str5 isEauqlToString:str6];//先判斷字符串的地址是否相同,如果相同,再判斷字符串是否相等。3> NSComparisonResult result = [Str5 compare:str6];switch (result){case NSOrderedAscending://阿詩瑪值小break;case NSOredredSame://字符串相等break;caes NSOrderedDeescending://阿詩瑪值大break;Deafault;break;}4、基本數據類型和字符串相互轉化基本數據類型(int、float、double、char)1)基本類型轉換成字符串int a = 10;[NSString stringWithForamt:@“%d”,a];float f = 2.4 -> @“%.1f”double d = 68.9999 -> @“%.4f”,dchar c = ‘c’ -> @“%c”,c2)字符串轉換成基本數據類型//floatValue、doubleValue、intValueNSString *str = @“32432”;int i = [str intValue];//@“i234324”這個轉換要注意,以上轉換不了3)將字符串中的字母轉換成大寫NSString *str = @“china”;NSString *str1 = [str1 uppercaseString];4)將字符串中的字母轉換成小寫NSString *str = @“china”;NSString *str1 = [str1 lowercaseString];5)將字符串的首字母轉成大写NSString *str = @“CHINA”;NSString *str = [str capitalizedString];5、多个数据拼接成一个字符串int year = 1988;int month = 4;int day = 15;NSString * date = [NSString stringWithFormat:@"%d年%d月%d日",year,month,day];NSLog(@"date %@",date);NSString * address = @"昌平区,金燕龙";NSString * dateAndAddrss = [NSString stringWithFormat:@"%@地址:%@",date,address];NSLog(@"dateAndAddress %@",dateAndAddrss);6、字符串的末尾追加新的字符NSString * str1 = @"www.";NSString * str2 = [str1 stringByAppendingString:@"itcast.cn"];NSLog(@"str2 %@ str1 %@",str2,str1);7、在制定的范围追加字符串NSString * str3 = @"www.cn";NSRange range = {4,0};// 4 location代表的时从哪个索引开始插入,length 代表的意思可以覆盖几个字符NSString * str4 = [str3 stringByReplacingCharactersInRange:range withString:@"itcast."];NSLog(@"str4 %@",str4);8、使用新的字符,替换原有的字符NSString * str5 = @"www.itcast.cn";NSString * str6 = [str5 stringByReplacingOccurrencesOfString:@"w" withString:@"a"];NSLog(@"str6 %@",str6);//5.在字符串中删除特定的某些字符 胡汉三 hhsNSString * str7 = @"www---.itcatst.--cn";NSString * str8 = [str7 stringByReplacingOccurrencesOfString:@"-" withString:@""];NSLog(@"str8 %@",str8);9、去掉字符串中的空格NSString * str9 = @"www .itcat .cn";NSString * str10 = [str9 stringByReplacingOccurrencesOfString:@" " withString:@""];NSLog(@"str10 %@",str10);10、讀取、寫入文件NSString *content = @“浙江冰雹,大如湯圓”;NSString *path = @“/Users/apple/Desktop/my.text”;NSError *error;1>將字符串寫入文件BOOL isWriteSuccess = [Content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];If (isWriteSuccess){NSLog (@“寫入文件成功,創建文件”);}else{NSLog (@“@“%@”,error”);}2> 從文件中讀取字符串NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:$error];If(error! = nil){NSLog(@“%@”,error);}else{NSLog(@“寫入成功%@”,str);}11、可變長度字符串的用法NSMutableString//原來的字符串沒有變,只是重新創建了一個新的字符串NSString *str = @“我喜歡”;NSString *str1 = [str stringByAppendingString:@“蘋果”];//兩種方法可改變原來長度字符串NSMutableString *mstr = [[NSMutableString alloc] init];//方法一NSMutableString *mstr = [[NSMutableString alloc] initWithCapacity:0];//方法二[mstr setString@“蘋果”];//是初始化字符串內容[mstr appendString:@“電腦”];//動態的給字符串末尾追加新值[mstr insertString:@“手機” atIndex:3];//在指定的索引位置插入字符串//刪去指定範圍的字符串NSRange range = {3,5};[mstr deleteCharactersInRange:rang];//替代其中的字符[mstr replaceCharactersInRange:range withString@“i love”];