2019独角兽企业重金招聘Python工程师标准>>>
#import <Foundation/Foundation.h>int main(int argc, const char * argv[])
{@autoreleasepool {NSString *str1=@"this is string A";NSString *str2=@"this is string B";NSString *temp;NSComparisonResult compareResult;//01.计算字符串中字符个数NSLog(@"字符串长度:%@的长度为%lu",str1,[str1 length]);//02.将一个字符串复制到另一字符串temp=[NSString stringWithString:str1];NSLog(@"字符串复制:%@",temp);//03.将一个字符串复制到另一字符串末尾temp=[str1 stringByAppendingString:str2];NSLog(@"字符串拼接:%@",temp);//04.验证两个字符串是否相等if([str1 isEqualToString:str2]){NSLog(@"字符串比较:%@与%@相同",str1,str2);}else{NSLog(@"字符串比较:%@与%@不相同",str1,str2);}//05.验证一个字符串是否小于,等于或大于另一字符串compareResult=[str1 compare:str2];if(compareResult==NSOrderedAscending){NSLog(@"字符串比较:%@<%@",str1,str2);}else if(compareResult==NSOrderedSame){NSLog(@"字符串比较:%@=%@",str1,str2);}else{NSLog(@"字符串比较:%@>%@",str1,str2);}//06.将字符串转为大写temp=[str1 uppercaseString];NSLog(@"字符串大写:%@",temp);//07.将字符串转为小写temp=[str1 lowercaseString];NSLog(@"字符串小写:%@",temp);//08.从字符串中取前3个字符temp=[str1 substringToIndex:3];NSLog(@"字符串截取:%@",temp);//09.从字符串中截取第5个字符以后的字符temp=[str1 substringFromIndex:5];NSLog(@"字符串截取:%@",temp);//10.截取从索引5到索引13的子字符temp=[[str1 substringFromIndex:5]substringToIndex:8];NSLog(@"字符串截取:%@",temp);temp=[str1 substringWithRange:NSMakeRange(5, 8)];NSLog(@"字符串截取:%@",temp);//11.从另一字符串查找一个字符串NSString *subStr=@"string A";NSRange subRange=[str1 rangeOfString:subStr];NSLog(@"字符串查找:%@位于%@第:%lu个字符,长度为:%lu",subStr,str1,subRange. location,subRange .length);subRange=[str2 rangeOfString:subStr];if(subRange.location==NSNotFound){NSLog(@"字符串查找:%@中不存在%@",str2,subStr);}else{NSLog(@"字符串查找:%@中存在%@",str2,subStr);}}return 0;
}
输出结果:
2014-02-12 22:41:43.890 05.字符串[1560:303]字符串长度:this is string A的长度为16
2014-02-12 22:41:43.892 05.字符串[1560:303]字符串复制:this is string A
2014-02-12 22:41:43.892 05.字符串[1560:303]字符串拼接:this is string Athis is string B
2014-02-12 22:41:43.892 05.字符串[1560:303]字符串比较:this is string A与this is string B不相同
2014-02-12 22:41:43.893 05.字符串[1560:303]字符串比较:this is string A<this is string B
2014-02-12 22:41:43.893 05.字符串[1560:303]字符串大写:THIS IS STRING A
2014-02-12 22:41:43.893 05.字符串[1560:303]字符串小写:this is string a
2014-02-12 22:41:43.893 05.字符串[1560:303]字符串截取:thi
2014-02-12 22:41:43.894 05.字符串[1560:303]字符串截取:is string A
2014-02-12 22:41:43.894 05.字符串[1560:303]字符串截取:is strin
2014-02-12 22:41:43.894 05.字符串[1560:303]字符串截取:is strin
2014-02-12 22:41:43.895 05.字符串[1560:303]字符串查找:string A位于this is string A第:8个字符,长度为:8
2014-02-12 22:41:43.895 05.字符串[1560:303]字符串查找:this is string B中不存在string A
Program ended with exit code: 0