#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str=[[NSString alloc]initWithString:@"a"];
NSString *str1=[[NSString alloc]initWithString:@"A"];
NSComparisonResult result= [str compare:str1];
// NSComparisonResult result= [str caseInsensitiveCompare:str1];
if (result==NSOrderedAscending) {
NSLog(@"str<str1");
}else if (result==NSOrderedDescending){
NSLog(@"str>str1");
}else{
NSLog(@"str=str1");
}
//2.比较两个字符串内容是否相同
if (str==str1) {
NSLog(@"两个字符串内容相同");
}else{
NSLog(@"两个字符串内容不相同");
}
//1.比较字符串对象是否相同
if ([str isEqualToString:str1]){
NSLog(@"两个字符串相同");
}else{
NSLog(@"两个字符串不相同");
}
}
return 0;
}
创建字符串
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建一个字符串常量
NSString *str=@"字符串";
// 创建一个空的字符串对象
//实例方法
NSString *str1=[[NSString alloc]init];
//类方法
NSString *str2=[NSString string];
//快速创建字符串
NSString *str3=[[NSString alloc]initWithString:@"zifuchuan"];
NSString *str4=[[NSString alloc]initWithString:str];
NSString *str5=[NSString stringWithString:@"zifuchuan"];
//格式化创建字符串
int num=23;
float num1=12.3;
NSString *str6=[[NSString alloc]initWithFormat:@"%d",num];
NSString *str7=[[NSString alloc]initWithFormat:@"这个数字是:%d或%f",num,num1];
NSLog(@"%@",str6);
NSLog(@"%@",str7);
NSString *str8=[NSString stringWithFormat:@"%d,%f",num,num1];
NSLog(@"%@",str8);
}
return 0;
}