OCbetway体育app: -- NSString字符串

#import <Foundation/Foundation.h>int main(int argc, const char *argv[]){ @autoreleasepool { /*____________字符串 NSString_______________________________*///创建 -> 属性 -> 方法//1.通过C的字符串 //c -> oc char *cString = "cString"; NSString *ocStr1 = [[NSString alloc]initWithUTF8String:cString]; NSString *ocStr2 = [NSString stringWithUTF8String:cString]; NSString *ocStr3 = [NSString stringWithCString:cString encoding:NSUTF8StringEncoding]; NSLog(@"%@",ocStr1); //oc -> c const char *cStr = [ocStr1 UTF8String]; //2.初始化 //1.实例方法 //快速创建:更改字符串 -> 更改了指针的指向地址 NSString *string = @"string"; string = @"新字符串,并不是在原来的字符串上修改";//NSString *str1 = [[NSString alloc]initWithString:@"字符串"]; NSString *str2 = [[NSString alloc]initWithFormat:@"%@哈哈%@",string,string]; NSLog(@"%@",str2); //2.类方法//NSString *str3 = [NSString stringWithString:@"类方法创建字符串"]; NSString *str4 = [NSString stringWithFormat:@"%d %@ %f",10,string,10.0];/*____________________________________________________________________*/ //3.属性//NSUInteger length = [string length]; NSLog(@"%ld",length); //4.方法 //判断字符串的内容是否相同 BOOL isEqual = [string isEqualToString:str2]; //判断字符串的地址是否相同 NSLog(@"%@",(str2 == str4)?@"地址相同":@"地址不同"); //比较字符串的大小 -> 升序 相同 降序 NSComparisonResult result = [@"aaa"caseInsensetiveCompare:@"bbb"]; switch { case NSOrderedAscending: NSLog; case NSOrderedSame: NSLog; case NSOrderdDescending: NSLog; break; default: break; }//❗️转换:大小字母转换 NSString *str5 = @"abcdefg hijklmn OPQRST UVWXYZ"; //1.全部变大写字母 NSLog(@"%@",[str5 uppercaseString]); //2.全部变小写字母 NSLog(@"%@",[str5 lowercaseString]); //3.首字母大写 -> 每个单词的首字母大写,其他小写 NSLog(@"%@",[str5 capitalizedString]); //str5还是原来的 NSLog(@"%@",str5); //❗️转化:基本数据类型 NSString *str6 = @"10086"; int intS = [str6 intValue]; NSInteger inteS = [str6integerValue]; float floS = [str6 floatValue]; double douS = [str6 doubleValue]; BOOL boolS = [str6 boolValue];/*________________________字符串的截取____________________________________*///1.分割 NSString *urlString = @"www.xiaoming.com";//将参数作为分隔符,将字符串分割成若干份NSArray *array = [urlString componentsSeparatedByString:@"."];NSLog(@"%@",array); //2.获取字符 char chaaracter = [urlString characterStIndex:4]; NSLog(@"%c",character); //3.截取 //截取到下标 NSLog(@"%@",[urlString substringToIndex:5]); //自下标截取 NSLog(@"%@",[urlString substringFromIndex:5]); //范围截取/* typedef stut _NSRange { NSUInteger location; //位置 -> 下标 -> 4 NSUInteger length; //长度 -> 长度 -> 7 }NSRange;*/ //c创建结构体 NSRange range1 = ; //oc创建结构体 NSRange range = NSMakeRange; NSLog(@"%@",[urlString substringWithRange:range]); //4.拼接 appending NSString *appString = @"拼接字符串:"; //拼接字符串 NSLog(@"%@",[appString stringByAppendingString:@"尾端添加字符串"]); //拼接格式化字符串 NSLog(@"%@",[appString stringByAppendingFormat:@"尾端添加格式化字符串%d",100]); //拼接扩展名 。pdf NSLog(@"%@",[appString stringByAppengdingPathExtension:@"dmg"]); //拼接路径 /path NSLog(@"%@",[appString stringByAppendingPathComponent:@"dmg"]);//5.查找 NSString *findString = @"/*路径*/ /Users/CORYIL/Lirary"; NSRange findResult = [findString rageOfString:@"Documents"]; //将结构体转化为字符串 NSStringFromXXX NSLog(@"%@",NSStringFromRange(findResult)); //6.替换 replacing //根据range替换 NSString *newString = [findString stringByReplacingCharactersInRange:findResult withString:@"呵呵"]; NSLog(@"%@",newString) //直接替换 NSLog(@"%@",[findString stringByReplacingOccurrencesOfString:@"Users" withString:@"用户们"]); } return 0;}

本文由必威发布于必威-编程,转载请注明出处:OCbetway体育app: -- NSString字符串

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。