// // main.m // NSString /** NSString 1.NSString 是一个不可以变的字符串对象 2.NSMutableString是一个可变字符串。 下面代码为字符串的:增、删、替换、查方法得演示。 */ #import <Foundation/Foundation.h> void MutableNSString(){ NSMutableString *mutableString =[NSMutableString stringWithFormat:@"我是可变字符串"]; //指定位置增加字符串 [mutableString insertString:@"增加" atIndex:([mutableString length])]; NSLog(@"%@",mutableString); //指定范围替换全部字符串 [mutableString replaceCharactersInRange:NSMakeRange(0, [mutableString length]) withString:@"替换后得字符串"]; NSLog(@"%@",mutableString); NSRange rang = [mutableString rangeOfString:@"字符串"]; [mutableString replaceCharactersInRange:rang withString:@"string"]; NSLog(@"%@",mutableString); //重新设置可变字符串内容 [mutableString setString:@"ABCDAa"]; //将A替换成E,方式为区分大小 [mutableString replaceOccurrencesOfString:@"A" withString:@"E" options:NSLiteralSearch range:NSMakeRange(0, [mutableString length])]; NSLog(@"%@",mutableString); //删除指定范围得字符串 NSRange deleteRang = [mutableString rangeOfString:@"a"]; //删除前最好判断是否存在,或者使用@try捕捉异常 [mutableString deleteCharactersInRange:deleteRang]; NSLog(@"%@",mutableString); } void BaseStringMeth(){ //字符串的基本方法 NSString *str1 = @"hello NSString "; //查询字符串得长度 NSUInteger length= [str1 length]; NSLog(@"length = %lu",length); // 转换大写 NSString *upperNSString = [str1 uppercaseString]; NSLog(@"%@",upperNSString); //转换小写 NSString *lowerNSString = [str1 lowercaseString]; NSLog(@"%@",lowerNSString); //字符换链接 NSString *appendString = @"study "; NSString *afterAppendString = [appendString stringByAppendingString:str1]; NSLog(@"%@",afterAppendString); //判断字符串是否相等 NSString *a = @"string a"; NSString *b = @"string b"; NSString *c = @"string a"; NSString *d = @"String a"; NSString *e = @"Strina a"; if([a isEqualToString:b] != YES){ NSLog(@"不相等"); } if([a isEqualToString:c]){ NSLog(@"相等"); } // 这个判断是严格区分大小写 if([a isEqualToString:d]){ NSLog(@"相等"); } //字符串比较大小,字符串得比较其实就是比较 NSString *compare1= @"1"; NSString *compare2 = @"2"; //- (NSComparisonResult)compare:(NSString *)string; 比较方法返回值是NSComparisonResult,他是一个枚举类型 //typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending}; if([compare1 compare:compare2] == NSOrderedAscending ){ NSLog(@"compare1 <compare2"); }else if ([compare1 compare:compare2] == NSOrderedSame ){ NSLog(@"compare1 ==compare2"); }else if ([compare1 compare:compare2] == NSOrderedDescending){ NSLog(@"compare1 >compare2"); } NSString *compare3= @"res1"; NSString *compare4 = @"RES1"; if([compare3 compare:compare4] == NSOrderedAscending ){ NSLog(@"compare3 <compare4"); }else if ([compare3 compare:compare4] == NSOrderedSame ){ NSLog(@"compare3 ==compare4"); }else if ([compare3 compare:compare4] == NSOrderedDescending){ NSLog(@"compare3 >compare4"); } //忽略大小写的比较 if([compare3 caseInsensitiveCompare:compare4] == NSOrderedAscending ){ NSLog(@"compare3 <compare4"); }else if ([compare3 caseInsensitiveCompare:compare4] == NSOrderedSame ){ NSLog(@"compare3 ==compare4"); }else if ([compare3 caseInsensitiveCompare:compare4] == NSOrderedDescending){ NSLog(@"compare3 >compare4"); } } void NSStringSearch(){ //字符串的查找 NSString *str1 = @"hello NSString "; NSString *hello = @"hello"; //查找hello字符串的位置 NSRange helloRang = [str1 rangeOfString:hello]; NSLog(@"hello location=%lu and length=%lu",helloRang.location,helloRang.length); NSString *hello2 = @"hello2"; NSRange helloRang2 = [str1 rangeOfString:hello2]; //找不到对应的字符串的时候系统会返回一个NSNotFound if(helloRang2.location == NSNotFound){ NSLog(@"在%@,中没有找到对应的字符串%@",str1,hello2); } /** 传入 NSStringCompareOptions 枚举的值 enum{ NSCaseInsensitiveSearch = 1,//不区分大小写比较 NSLiteralSearch = 2,//区分大小写比较 NSBackwardsSearch = 4,//从字符串末尾开始搜索 NSAnchoredSearch = 8,//搜索限制范围的字符串 NSNumbericSearch = 64//按照字符串里的数字为依据,算出顺序。例如 Foo2.txt < Foo7.txt < Foo25.txt //以下定义高于 mac os 10.5 或者高于 iphone 2.0 可用 NSDiacriticInsensitiveSearch = 128,//忽略 "-" 符号的比较 NSWidthInsensitiveSearch = 256,//忽略字符串的长度,比较出结果 NSForcedOrderingSearch = 512//忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending //以下定义高于 iphone 3.2 可用 NSRegularExpressionSearch = 1024//只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch } */ //指定范围进行查找。我们可以通过options来确定查询得方式。从头开始还是从尾开始 NSRange nsstringRang= [str1 rangeOfString:@"NSString" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [str1 length])]; NSLog(@"location=%lu,length=%lu",nsstringRang.location,nsstringRang.length); } //字符串的截取 void SubString(){ //字符串截取 NSString *str1 = @"hello NSString "; //从第5个字符开始截取到最后 NSString *subString = [str1 substringFromIndex:5]; NSLog(@"%@",subString);//输出NSString NSRange rang = {0,5}; //指定范围得截取 NSString *subByRang = [str1 substringWithRange:rang]; NSLog(@"%@",subByRang);//输出hello //从0开始截取到指定的位置 NSString *subToPosition= [str1 substringToIndex:5]; NSLog(@"%@",subToPosition);//输出hello } //字符串的创建 void CreateNSString(){ //字符串的创建 NSString *str1 = @"hello NSString "; //静态方法创建字符串, NSString *str2 = [NSString stringWithFormat:@"NSString%i",2]; //动态创建字符串,由于动态创建计数器会+1因此我们需要自己释放,str3初始化时没有相应的值 NSString *str3 = [[NSString alloc]init]; NSLog(@"%@",str3); [str3 release]; } int main(int argc, const char * argv[]) { @autoreleasepool { CreateNSString(); BaseStringMeth(); SubString(); NSStringSearch; MutableNSString(); } return 0; }
时间: 2024-11-08 01:50:54