#import <Foundation/Foundation.h>
int main(intargc, const char * argv[]) {
//类方法:2种
//1.将C串转为OC串[NSStringstringWithUTF8String:str];
char *str = "sdjfiehgs";
NSString*str1 = [NSString stringWithUTF8String:str];
NSLog(@"str1 = %@",str1);
//2.拼接为一个新串[NSStingstringWithFormat:@"%@--%d",name,age];
NSString *name = @"小明";
int age = 20;
NSString *str2 = [NSString stringWithFormat:@"我叫%@,今年%d岁了",name,age];
NSLog(@"str2 = %@",str2);
//对象方法:4种
//1.字符串长度[str.lenth];
NSString*str3 = @"djfiefskln";
NSUInteger len = str3.length;
NSLog(@"len = %lu",len);
//2.得到指定下标的元素[str4 characterAtIndex:i];
NSString*str4 = @"djfiefskln";
char c = [str4 characterAtIndex:3];
NSLog(@"c = %c",c);
//3.字符串比较排序:NSComparisonResult res = [str5compare:str6];
//typedefNS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L,NSOrderedSame, NSOrderedDescending};
NSString*str5 = @"jiofskln";
NSString*str6 = @"djfiefskln";
NSComparisonResult res = [str5 compare:str6];//结果是枚举值
switch (res) {
case NSOrderedAscending:
NSLog(@"str5比str6小");
break;
case NSOrderedSame:
NSLog(@"一样大");
break;
case NSOrderedDescending:
NSLog(@"str5比str6大");
break;
default:
break;
}
//4.字符串内容是否相同:BOOL res1 = [str7isEqualToString:str8];
NSString*str7 = @"djfiefskln";
NSString*str8 = @"djfiefsklm";
BOOL res1 = [str7 isEqualToString:str8];
if (res1) {
NSLog(@"YES 字符串%@和字符串%@的内容相同",str7,str8);
}else {
NSLog(@"NO 字符串%@和字符串%@的内容不相同",str7,str8);
}
return 0;
}