1、创建常量字符串。
NSString *astring = @"This is a String!";2、创建空字符串,给予赋值。
NSString *astring = [[NSString alloc] init]; astring = @"This is a String!";3、在以上方法中,提升速度:initWithString方法
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring);4、用标准c创建字符串:initWithCString方法
char *Cstring = "This is a String!"; NSString *astring = [[NSString alloc] initWithCString:Cstring]; NSLog(@"astring:%@",astring);5、创建格式化字符串:占位符(由一个%加一个字符组成)
int i = 1; int j = 2; NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];6、创建临时字符串
NSString *astring; astring = [NSString stringWithCString:"This is a temporary string"];7、从文件创建字符串
NSString *path = [[NSBundle mainBundle] pathForResource:@"astring.text"ofType:nil]; NSString *astring = [[NSString alloc] initWithContentsOfFile:path]; NSLog(@"astring:%@",astring);8、用字符串创建字符串,并写入到文件
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring); NSString *path = @"astring.text"; [astring writeToFile: path atomically: YES];注:此路径path只只是示意,真实路径并非如此
char string1[] = "string!"; char string2[] = "string!"; if(strcmp(string1, string2) == 0){ NSLog(@"1"); }10、isEqualToString方法
NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 isEqualToString:astring02]; NSLog(@"result:%d",result);11、compare方法(comparer返回的三种值)
NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] == NSOrderedSame; //NSOrderedSame判断两者内容是否相同 NSLog(@"result:%d",result);
NSString *astring01 = @"This is a String!"; NSString *astring02 = @"this is a String!"; BOOL result = [astring01 compare:astring02] == NSOrderedAscending; //NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真) NSLog(@"result:%d",result);
NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] == NSOrderedDescending;//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) NSLog(@"result:%d",result);12、不考虑大小写比较字符串
1.NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 caseInsensitiveCompare:astring02] == NSOrderedSame;//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) NSLog(@"result:%d",result);
2.NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame;//NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。 NSLog(@"result:%d",result);13、输出大写或者小写字符串
NSString *string1 = @"A String"; NSString *string2 = @"String"; NSLog(@"string1:%@",[string1 uppercaseString]);//大写 NSLog(@"string2:%@",[string2 lowercaseString]);//小写 NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小14、-rangeOfString: //查找字符串某处是否包含其它字符串
NSString *string1 = @"This is a string"; NSString *string2 = @"string"; NSRange range = [string1 rangeOfString:string2]; int location = range.location; int leight = range.length; NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]]; NSLog(@"astring:%@",astring); [astring release];15、-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringToIndex:3]; NSLog(@"string2:%@",string2);16、-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringFromIndex:3]; NSLog(@"string2:%@",string2);17、-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringWithRange:NSMakeRange(0<pre name="code" class="objc"> NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 insertString:@"Hi! " atIndex:0]; NSLog(@"String1:%@",String1);
NSMutableString *String; String = [NSMutableString stringWithCapacity:40];19、-appendString: and -appendFormat: //把一个字符串接在另一个字符串的末尾
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 appendString:@", I will be adding some character"]; [String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]]; NSLog(@"String1:%@",String1);20、-insertString: atIndex: //在指定位置插入字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 setString:@"Hello Word!"]; NSLog(@"String1:%@",String1);22、-replaceCharactersInRange: withString: //用指定字符串替换字符串中某指定位置、长度的字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"]; NSLog(@"String1:%@",String1);23、-hasPrefix: //检查字符串是否以另一个字符串开头
NSString *String1 = @"NSStringInformation.txt"; [String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO"); [String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");24、扩展路径
NSString *Path = @"~/NSData.txt"; NSString *absolutePath = [Path stringByExpandingTildeInPath]; NSLog(@"absolutePath:%@",absolutePath); NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);25、文件扩展名
[str length]; //这个返回一个NSUInteger 无符号的整型,占位符用 %lub、根据索引获得单个字符
unichar c = [str characterAtIndex:2]; 返回单个字符 占位符用 %cc、根据索引获得字符串的一个子串
[str substringFromIndex:3]; 从给定的索引开始(包含该索引位置)截取到字符串末尾 [str substringToIndex:3]; 截取字符串到给定索引的位置结束,(不包含该索引位置)d、截取字符串的某一段
NSRange rang = {4,6}; 起始位置是4,从第四个截取长度为6 //location (起始索引的位置,包含该索引) length(要截取的长度) NSString * tmpStr3 = [str3 substringWithRange:rang];e、获得一个字符串的索引位置
[str1 rangeOfString:tmpStr1]; //返回NSRange 可以用NSStringFromRange(tmpRange) 输出,tmpRange.location == NSNotFound 判断是否找到字符串tmpStr27、字符串判断
a、//用来判断字符串是否为空 str == nil || str.length == 0 b、判断字符串是否已指定的内容开头 [str hasPrefix:@“abc”]; 返回 BOOL c、判断字符串是否以指定的内容结尾 [str hasSuffix:@“.txt"];返回BOOL 在开发中常用于判断文件格式 d、判断两个字符串是否相等 str1 == str2 使用 ==号 判断两个字符串 实际上是判断的字符串地址是否相同 [str1 isEqualToString:str2] 判断字符串内容是否相等 e、compare 是isEqualToString的增强版本 NSComparisonResult result = [strTmp1 compare:strTmp2]; 返回两个字符串是升序 降序还是相等 NSOrderedAscending 升序 NSOrderedSame 相等 NSOrderedDescending 降序28、类型转换
a、int类型换换成字符串 NSString *str=[NSString stringWithFormat:@“%d”,a]; b、float -> NSString NSString * str = [NSString stringWithFormat:@“%.2f",f]; //%.2f 表示两位小数 . double和float一样 c、char - > NSString NSString * str = [NSString stringWithFormat:@“%c",c]; d、NSString 转换成普通数据类型 [str intValue]; [str floatValue]; [str doubleValue]; e、将字符串中的字母转换成大写 [str uppercaseString]; 转成大写 [str lowercaseString]; 转成小写 f、将首字母大写 [str capitalizedString];29、字符串重组
NSString * date = [NSString stringWithFormat:@"%d年%d月%d日”,year,month,day];b、字符串的末尾追加新的字符
[str stringByAppendingString:@“我是追加的"];c、在制定的范围追加字符串
NSRange range = {4,0}; 从第四个索引开始,覆盖0个字符 location代表的时从哪个索引开始插入,length 代表的意思可以覆盖几个字符 NSString * str4 = [str stringByReplacingCharactersInRange:range withString:@“插入的"];d、使用新的字符,替换原有的字符
[str stringByReplacingOccurrencesOfString:@"w" withString:@“a"];e、在字符串中删除特定的某些字符
[str stringByReplacingOccurrencesOfString:@"-" withString:@“"];//将想要删除的字符 替换成空的字符</span>
[str stringByReplacingOccurrencesOfString:@" " withString:@“"];//将空格替换成空字符
NSString * path = @"/Users/xxxx/Desktop/mytest.txt"; NSError * error; NSString * str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; if (error != nil) { NSLog(@"error message %@",error); } else { NSLog(@"str content %@",str); }
NSError * error; BOOL isWriteSuccess = [content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]; if(isWriteSuccess) { NSLog(@"文件创建成功"); } else { NSLog(@"error %@",error); }31、从文件中读取字符串
NSString * path = @"/Users/xxxx/Desktop/mytest.txt"; NSError * error; NSString * str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; if (error != nil){ NSLog(@"error message %@",error); }else{ NSLog(@"str content %@",str); }32、可变长度字符串 NSMutableString
NSMutableString * muStr = [[NSMutableString alloc] initWithCapacity:0]; 初始化一个空字符串 [muStr setString:@“aa"];//给字符串设置内容 //动态的给字符串末尾追加新值 [muStr appendString:@“cc"]; //在指定的索引位置,插入字符串 [muStr insertString:@"bb" atIndex:2]; //删除指定范围的字符串 //你要删除的长度,一定不能够超出字符串的的长度,导致异常Range or index out of bounds NSRange range = {3,7}; [muStr deleteCharactersInRange:range]; //替换 [muStr replaceCharactersInRange:range withString:@“content’"];