总览:
•创建常量字符串
•创建空字符串
•initWithString方法
•用标准C创建字符串
•创建格式化字符串
•创建临时字符串
•从文件创建字符串
•用字符串创建字符串,并写入到文件
实现:
创建常量字符串
NSString*aString = @”This is a String”;
NSLog(@”string = %@”,aString);
打印:sring =ThisisaString
创建空字符串,给予赋值
NSString *aString = [[NSStringalloc]init];
aString = @”This is a String”;
NSLog(@”string = %@”,aString);
打印:sring =ThisisaString
initWithString方法
作用:相对上面的方法可提升速度
NSString *astring = [[NSStringalloc]initWithString:@"This is aString!"];
用标准C创建字符串
方法:initWithCString
char *cString = ”This is a String”;
NSString *astring = [[NSStringalloc]initWithCString:Cstring];
NSLog(@”string = %@”,aString);
打印:sring =ThisisaString
创建格式化字符串
占位符:由一个%加一个字符组成。
inti = 1;
intj = 2;
NSString *aString= [[NSStringalloc]initWithString:[NSStringstringWithFormat:@“i is %d, j is %d”,i,j]];
NSLog(@”string = %@”,aString);
打印:string=i is 1, j is 2
创建临时字符串
NSString *aString;
aString= [NSStringstringWithCString:“Thisis a temporary string”];
NSLog(@”aString = %@”,aString);
打印:aString = This is a temporary string
从文件创建字符串
获取文件路径
NSString*path = [[NSBundle mainBundle]pathForResource:@“aString.text”ofType:nil];
NSString*aString= [[NSStringalloc]initWithContentsOfFile:path];
用字符串创建字符串
NSString*aString= [[NSStringalloc]initWithString:@“Thisis a String!”];
保存到的文件
NSString*path =@“astring.text”;
写入文件
[aStringwriteToFile: path atomically: YES];
char string1[] = “string!”;
char string2[] =“string!”;
if(strcmp(string1, string2) == 0) {
NSLog(@“1”);
}
说明:strcmp()用来比较字符串(区分大小写),其原型为:
intstrcmp(constchar *s1,constchar *s2);
比较两个字符串是否相等,返回布尔类型BOOL
NSString *aString01= @“This is a String!”;
NSString *aString02= @“This is a String!”;
BOOL result = [aString01isEqualToString:aString02];
comparer返回的三种值:
NSString *aString01= @“This is a String!”;
NSString*aString02= @“This is a String!”;
BOOL result = [aString01 compare:aString02] ==NSOrderedSame;
if (result){
//为真
NSLog(@”真”);
}
NSString *aString01= @"This is aString!";
NSString*aString02= @"this is aString!";
BOOL result = [aString01 compare:aString02] ==NSOrderedAscending;
if (result){
//为真
NSLog(@”真”);
}
NSString *aString01= @"this is a String!";
NSString *aString02= @"This is aString!";
BOOL result = [aString01 compare:aString02] ==NSOrderedDescending;
if (result){
//为真
NSLog(@”真”);
}
NSString *astring01 = @"thisis a String!";
NSString*astring02 = @"This isa String!";
BOOL result = [astring01caseInsensitiveCompare:astring02] ==NSOrderedSame;
//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
NSString *astring01 = @"thisis a String!";
NSString *astring02 = @"Thisis a String!";
BOOL result = [astring01compare:astring02options:NSCaseInsensitiveSearch |NSNumericSearch] ==NSOrderedSame;
//NSCaseInsensitiveSearch:不区分大小写比较NSLiteralSearch:进行完全比较,区分大小写NSNumericSearch:比较字符串的字符个数,而不是字符值。
NSString *string1 = @"AString";
NSString*string2 = @"String";
NSLog(@"string1:%@",[string1uppercaseString]);//大写
打印:string1: A STRING
NSLog(@"string2:%@",[string2lowercaseString]);//小写
打印:string2: string
NSLog(@“string2:%@”,[string2capitalizedString]);//首字母大写
打印:String
查找字符串某处是否包含其它字符串。
NSString *string1 =@“This is astring”;
NSString*string2 =@“string”;
NSRangerange = [string1rangeOfString:string2];
intlocation = range.location;//字符串所在位置
NSString*aString= [[NSStringalloc]initWithString:[NSStringstringWithFormat:@“Location:%i,Leight:%i”,location,leight]];
NSLog(@”aString:%@",aString);
打印:aString:Location:10,Leight:6
从字符串的开头一直截取到指定的位置,但不包括该位置的字符。
NSString *string1 =@“This is astring”;
NSString*string2 = [string1substringToIndex:3];
NSLog(@"string2:%@",string2);
打印:string2: Thi
以指定位置开始(包括指定位置的字符),并包括之后的全部字符。
NSString *string1 =@“This is astring”;
NSString *string2 = [string1substringFromIndex:3];
NSLog(@“string2:%@”,string2);
打印:string2: s is a string
按照所给出的位置,长度,任意地从字符串中截取子串。
NSString *string1 =@“This is astring”;
NSString*string2 = [string1substringWithRange:NSMakeRange(0, 4)];
NSLog(@“string2:%@”,string2);
打印:string2: This
按照固定长度生成空字符串。
NSMutableString *String;
String = [NSMutableStringstringWithCapacity:40];
这是初始化一个可变的字符串
把一个字符串接在另一个字符串的末尾。
NSMutableString *String1 = [[NSMutableStringalloc]initWithString:@"This is aNSMutableString"];
[String1 appendString:@", Yes"];
[String1 appendFormat:[NSStringstringWithFormat:@",itis"]];
NSLog(@"String1:%@",String1);
打印:String1: :This isaNSMutableString, Yes, it is
在指定位置插入字符串。
NSMutableString *String1 = [[NSMutableStringalloc]initWithString:@“Thisis a NSMutableString”];
[String1 insertString:@“Hi!” atIndex:0];//在第0个位置插入
NSLog(@“String1:%@”,String1);
打印:String1: Hi! This is aNSMutableString
替换原来的值。
NSMutableString *String1 = [[NSMutableStringalloc]initWithString:@"This is aNSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@“String1:%@”,String1);
打印:String1: Hello Word!
用指定字符串替换字符串中某指定位置、长度的字符串。
NSMutableString *String1 = [[NSMutableStringalloc]initWithString:@“Thisis a NSMutableString”];
[String1 replaceCharactersInRange:NSMakeRange(0, 4)withString:@“That”]; //从第0个位置开始长度是4(This)用That来替换
NSLog(@“String1:%@”,String1);
打印:String1: That is a NSMutableString
检查字符串是否以另一个字符串开头和是否以一个字符串结尾。
NSString *String1 =@“NSStringInformation.txt”;[String1 hasPrefix:@“NSString”]= = 1 ? NSLog(@“YES”): NSLog(@“NO”);
打印:YES
[String1 hasSuffix:@“.txt”]= = 1 ? NSLog(@“YES”): NSLog(@“NO”);
打印:YES
说明:用到的“?:” 是三目运算符