#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *str1 = @"This is string 1";
NSString *str2 = @"This is string 2";
NSString *ret;
NSComparisonResult compareResult;
//计算字符串的长度
NSLog(@"lenght is %lu",str1.length);
//将一个字符串复制到另外一个字符串
ret = [NSString stringWithString:str1];
NSLog(@"copy %@",ret);
//将一个字符串复制到另外一个字符串的末尾
str2 = [str1 stringByAppendingString:str2];
NSLog(@"concatentation : %@",str2);
if([str2 isEqualToString:ret] == YES){
NSLog(@"str2 == ret");
}else{
NSLog(@"str2 != ret");
}
//验证一个字符串是否大于,等于,小于,另外一个字符串
compareResult = [str1 compare:str2];
if (compareResult == NSOrderedAscending) {
NSLog(@"str1 < str2");
}else if(compareResult == NSOrderedSame){
NSLog(@"str1 == str2");
}else if(compareResult == NSOrderedDescending)
NSLog(@"str1 > str2 ");
//将字符串转换成大写
ret = [str1 uppercaseString];
NSLog(@"uppercase conversion %s",[ret UTF8String]);
//将字符串转换成小写
ret = [str1 lowercaseString];
NSLog(@"lowercase conversion %@",ret);
//将字符串大小写转换原字符串不会被改变 ,只是返回一个当前字符串的大写。
//从字符串中提取前3个字符
ret = [str1 substringToIndex:3];
NSLog(@"First 3 chars of str1 %@",ret);
//提取从索引5开始直到结尾的字符串
ret = [str1 substringFromIndex:5];
NSLog(@"chars form index 5 of str1 %@",ret);
//提取从索引5开始到索引13的字符串(6个字符)
ret = [[str1 substringFromIndex:8] substringToIndex:6];
NSLog(@"chars from index 8 through 13 :%@",ret);
//更简单的方法
NSRange subRange;
ret = [str1 substringWithRange:NSMakeRange(8, 6)];
NSLog(@"chars form index 8 through 13 %@",ret);
//从一个字符串中查找另外一个字符串
subRange = [str1 rangeOfString:@"string 1"];
NSLog(@"string is at index %lu, lenght is %lu",subRange.location,subRange.length);
subRange = [str1 rangeOfString:@"string 2"];
if (subRange.location == NSNotFound) {
NSLog(@"String is Found");
}else{
NSLog(@"string is at index %lu, lenght is %lu",subRange.location,subRange.length);
}
}
return 0;
}