1、创建常量字符串
NSString *str = @"Hello World!";
2、创建空字符串,给予赋值
NSString *str = [[NSString alloc] init];
str = @"Hello World!";
[str release];
3、initWithString方法
NSString *str = [[NSString alloc] initWithString:@"Hello World!"];
[str release];
4、用标准c创建字符串:initWithCString方法
char *Cstr = "This is a String!";
NSString *str = [[NSString alloc] initWithCString:Cstr];
[str release];
5、创建格式化字符串:占位符%
int i = ;
int j = ;
NSString *str = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];
[str release];
6、创建临时字符串
NSString *str = [NSString stringWithCString:"Hello World"];
7、从文件创建字符串
NSString *path = [[NSBundlemainBundle] pathForResource:@"str.text"ofType:nil];
NSString *str = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"str:%@",str);
[str release];
8、用字符串创建字符串,并写入到文件
NSString *str = [[NSString alloc] initWithString:@"This is a String!"];
NSString *path = @"str.text";
[str writeToFile: path atomically: YES];
[str release];
、isEqualToString方法
NSString *str01 = @"This is a String!";
NSString *str02 = @"This is a String!";
BOOL result = [str01 isEqualToString:str02];
NSLog(@"result:%d",result);
10、compare方法(comparer返回的三种值)
47 //NSOrderedSame判断两者内容是否相同
NSString *str01 = @"This is a String!";
NSString *str02 = @"This is a String!";
BOOL result = [str01 compare:str02] == NSOrderedSame;
NSLog(@"result:%d",result);
//NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)
NSString *str01 = @"This is a String!";
NSString *str02 = @"this is a String!";
BOOL result = [str01 compare:str02] == NSOrderedAscending;
NSLog(@"result:%d",result);
//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
NSString *str01 = @"this is a String!";
NSString *str02 = @"This is a String!";
BOOL result = [str01 compare:str02] == NSOrderedDescending;
NSLog(@"result:%d",result);
11、不考虑大小写比较字符串
NSString *str01 = @"this is a String!";
NSString *str02 = @"This is a String!";
BOOL result = [str01 caseInsensitiveCompare:str02] == NSOrderedSame; //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,str02小于str01为真)
NSLog(@"result:%d",result);
NSString *str01 = @"this is a String!";
NSString *str02 = @"This is a String!";
BOOL result = [str01 compare:str02
options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame;
NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。
NSLog(@"result:%d",result);
12、输出大写或者小写字符串
NSString *string1 = @"String";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//大写
NSLog(@"string2:%@",[string2 lowercaseString]);//小写
NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小
13、-rangeOfString: 查找字符串某处是否包含其它字符串
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *str = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"str:%@",str);
[str release];
14、-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:];
NSLog(@"string2:%@",string2);
15、-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:];
NSLog(@"string2:%@",string2);
16、-substringWithRange: 按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(, )];
NSLog(@"string2:%@",string2);
17、-stringWithCapacity: 按照固定长度生成空字符串
NSMutableString *String;
String = [NSMutableString stringWithCapacity:];
18、-appendString: and -appendFormat: 把一个字符串接在另一个字符串的末尾
NSMutableString *str1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[str1 appendString:@", I will be adding some character"];
[str1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];
NSLog(@"str1:%@",str1);
19、-insertString: atIndex: 在指定位置插入字符串
NSMutableString *str1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[str1 insertString:@"Hi! " atIndex:];
NSLog(@"str1:%@",str1);
20、-setString:
NSMutableString *str = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[str setString:@"Hello Word!"];
NSLog(@"str:%@",str);
21、-replaceCharactersInRange: withString: 用指定字符串替换字符串中某指定位置、长度的字符串
NSMutableString *str = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[str replaceCharactersInRange:NSMakeRange(, ) withString:@"That"];
NSLog(@"str:%@",str);
22、-hasPrefix: 检查字符串是否以另一个字符串开头
NSString *str = @"NSStringInformation.txt";
[str hasPrefix:@"NSString"] = = ? NSLog(@"YES") : NSLog(@"NO");
[str hasSuffix:@".txt"] = = ? NSLog(@"YES") : NSLog(@"NO");
23、扩展路径
NSString *Path = @"~/NSData.txt";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
24、文件扩展名
NSString *Path = @"~/NSData.txt";
NSLog(@"Extension:%@",[Path pathExtension]);