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