NSString *str1 = @"This is string A";
NSString *str2 = @"This is string b";
NSString *res;
NSComparisonResult compareResult;//声明保存字符串比较操作的结果
NSLog(@"Length of str1: %lu",(unsignedlong)[str1 length]);//字符串的长度检测方法
res = [NSStringstringWithString:str1];// 引用字符串
NSLog(@"copy : %@", res);
str2 = [str1 stringByAppendingString:str2];// 连接字符串的方法
NSLog(@"Concatentation:%@", str2);
if ([str1 isEqualToString:res] == YES) {
NSLog(@"str1 == res ");
}else
NSLog(@"str1 != res");
compareResult = [str1 compare:str2];
if (compareResult == NSOrderedAscending) {
NSLog(@"str1 < str2");
}elseif (compareResult == NSOrderedSame)
NSLog(@"str1 == str2");
else//NsorderedDescending 大于
NSLog(@"str1 > str2");
res = [str1 uppercaseString];//转字符串为大写
NSLog(@"Uppercase conversion: %s",[res UTF8String]);
res = [str1 lowercaseString];//转字符串为小写,并未改变原本的字符串
NSLog(@"lowercase conversion:%@",res);
NSLog(@"Original string : %@",str1);
////////////////////////////////////////////////////////////////////////////////////////////////例子2
NSString *str1 = @"This is string A";
NSString *str2 = @"This is string B";
NSString *res;
NSRange subRange;// 注意不是对象变量,是结构变量
res = [str1 substringToIndex:3];//substringToIndex 索引数是0开始,所以提取的是字符 0,1,2,如果索引数无效,会获得 Range(范围) or index out of bounds 的出错信息
NSLog(@"First 3 chars of str1:%@",res);
res = [str1 substringFromIndex:5];// 输出从索引字符开始到字符串结尾
NSLog(@"Chars from index 5 of str1 :%@", res);
res = [[str1 substringFromIndex:8] substringToIndex:6];//复合用法
NSLog(@"Chars from index 8 throughe 13 :%@", res);
res = [str1 substringWithRange:NSMakeRange(8, 6)];//接受范围函数
NSLog(@"Chars from index 8 throughe 13 :%@ ", res);
subRange = [str1 rangeOfString:@"string A"];//在一个字符串中查找另一个字符串的方法
NSLog(@"string is at index %lu, length is %lu", (unsigned long)subRange.location,(unsigned long)subRange.length);
subRange = [str1 rangeOfString:@"string B"];
if (subRange.location == NSNotFound)
NSLog(@"string not found");
else
NSLog(@"String is at index %lu, length is %lu", (unsigned long)subRange.location, (unsigned
long)subRange.length);
////////////////////////////////////////////////////////////////////////////////////////////////例子3
NSString *str1 = @"This is string A";
NSString *search, *replace;
NSMutableString *mstr;
NSRange substr;
mstr = [NSMutableStringstringWithString:str1 ];
NSLog(@"%@",mstr);
[mstr insertString:@" mutable"atIndex: 7]; //指定位置插入字符
NSLog(@"%@",mstr);
[mstr insertString:@" and string B"atIndex: mstr.length];//配合.length方法在末尾插入字符
NSLog(@"%@",mstr);
[mstr appendString:@" and string C"];//简化的上一条操作
NSLog(@"%@",mstr);
[mstr deleteCharactersInRange:NSMakeRange(16, 13)];//从索引数16(字符串第17个字符)开始删除13个字符
NSLog(@"%@ 删除后",mstr);
substr = [mstr rangeOfString:@"string B and"];
if (substr.location != NSNotFound) {
[mstr deleteCharactersInRange: substr];
NSLog(@"%@", mstr);
}
[mstr setString:@"This is string A"];//直接设置可变字符串
NSLog(@"%@ ", mstr);
[mstr replaceCharactersInRange: NSMakeRange(8, 8) withString:@"a mutable string "];
NSLog(@"%@",mstr); //范围替换字符串
search = @"This is";
replace = @"An example of ";
substr = [mstr rangeOfString: search];
if (substr.location != NSNotFound) {
[mstr replaceCharactersInRange:substr withString:replace];
NSLog(@"%@",mstr);
}
search = @"a";
replace = @"X";//该为'空'可以删除字符串
substr = [mstr rangeOfString: search];
// while (substr.location != NSNotFound) {
// [mstr replaceCharactersInRange:substr withString: replace];
// substr = [mstr rangeOfString: search];
// }
[mstr replaceOccurrencesOfString:search withString:replace options: 2 range:NSMakeRange(0, mstr.length)];//options:1 不区分大小写, 2 区分大小写
NSLog(@"%@",mstr);
|