zoukankan      html  css  js  c++  java
  • 字符串与常用目标互换

    1、OC 字符串与 C 字符串的相互转换

    // OC 字符串转 C 字符串
    const char *CString = [@"bei jing ni hao" UTF8String];
    
    // C 字符串转 OC 字符串
    NSString *OCString1 = [[NSString alloc] initWithCString:"ni hao" encoding:NSUTF8StringEncoding];
    NSString *OCString2 = [[NSString alloc] initWithUTF8String:"ni hao"];
    
    NSString *OCString3 = [NSString stringWithCString:"ni hao" encoding:NSUTF8StringEncoding];
    NSString *OCString4 = [NSString stringWithUTF8String:"ni hao"];
    

    2、NSString 与 NSData 的相互转换

    NSString *string = @"bei jing nin hao";
    
    // NSString 转 NSData
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    // data 类型数据以十六进制的形式打印出
    NSLog(@"%@", data);
    
    // data 类型数据以字符串的形式打印出
    NSLog(@"%s", data.bytes);
    
    // NSData 转 NSString
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    

    3、NSString 与 NSNumber 的相互转换

    NSNumber *num1 = @10.456;
    
    // NSNumber 转换 NSString
    NSString *str = [num1 stringValue];
    
    // NSString 转 NSNumber
    NSNumber *num2 = [NSNumber numberWithFloat:[str floatValue]];
    

    4、NSString 与 NSDate 的相互转换

    NSDateFormatter *dfm = [[NSDateFormatter alloc] init];
    dfm.dateFormat = @"yyyy年MM月dd日 HH点mm分ss秒";
    dfm.locale = [NSLocale localeWithLocaleIdentifier:@"GMT+8"];
    
    // NSString 转 NSDate
    NSDate *date = [dfm dateFromString:@"2016年05月24日 17点38分03秒"];
    
    // NSDate 转 NSString
    NSString *str = [dfm stringFromDate:date];
    

    5、NSString 与 Class 的相互转换

    // NSString 转 Class
    Class ClassName = NSClassFromString(@"Student");
    
    Student *stu = [[ClassName alloc] init];
    [stu studentTest];
    
    // Class 转 NSString
    NSString *strintFromClass = NSStringFromClass([Student class]);
    

    6、NSString 与 方法名 的相互转换

    // NSString 转 方法名
    SEL selName = NSSelectorFromString(@"studentTest");
    
    // 消除 performSelector: 方法警告
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    
    Student *stu = [[Student alloc] init];
    [stu performSelector:selName];
    
    #pragma clang diagnostic pop
    
    // 方法名 转 NSString
    NSString *stringFromSel = NSStringFromSelector(@selector(studentTest));
    

    7、NSString 与 NSProtocol 的相互转换

    // NSString 转 NSProtocol
    Protocol *ptlName = NSProtocolFromString(@"NSCopying");
    
    Student *stu = [[Student alloc] init];
    BOOL conforms = [stu conformsToProtocol:ptlName];
    
    // NSProtocol 转 NSString
    NSString *stringFromProtocol = NSStringFromProtocol(@protocol(NSCopying));
    

    8、NSString 与 NSRange 的相互转换

    NSRange range4 = NSMakeRange(9, 4);
    
    // NSRange 转 NSString
    NSString *stringFronRange = NSStringFromRange(range4);
    
    // NSString 转 NSRange
    NSRange range5 = NSRangeFromString(stringFronRange);
    

    9、NSString 与 NSPoint 的相互转换

    NSPoint point5 = NSMakePoint(9, 4);
    
    // NSPoint 转 NSString
    NSString *stringFronPoint = NSStringFromPoint(point5);
    
    // NSString 转 NSPoint
    NSPoint point6 = NSPointFromString(stringFronPoint);
    

    10、NSString 与 NSSize 的相互转换

    NSSize size5 = NSMakeSize(9, 4);
    
    // NSSize 转 NSString
    NSString *stringFronSize = NSStringFromSize(size5);
    
    // NSString 转 NSSize
    NSSize size6 = NSSizeFromString(stringFronSize);
    

    11、NSString 与 NSRect 的相互转换

    NSRect rect5 = NSMakeRect(9, 4, 4, 9);
    
    // NSRect 转 NSString
    NSString *stringFronRect = NSStringFromRect(rect5);
    
    // NSString 转 NSRect
    NSRect rect6 = NSRectFromString(stringFronRect);
    

    12、生成随机数

    // 生成 15 位长度的数字字母组合的随机数
    - (NSString *)generateTradeNO {
    
    	static int kNumber = 15;
    
    	NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	NSMutableString *resultStr = [[NSMutableString alloc] init];
    	srand((unsigned)time(0));
    	for (int i = 0; i < kNumber; i++)
    	{
    		unsigned index = rand() % [sourceStr length];
    		NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
    		[resultStr appendString:oneStr];
    	}
    
    	return resultStr;
    }
    
  • 相关阅读:
    【欧拉函数】BZOJ2190-[SDOI2012]longge的数学问题
    【AC自动机+DP】USACO2012 JAN GOLD_Video Game Combos
    【斜率优化】BZOJ1010 [HNOI2008]玩具装箱toy
    【二维单调队列】BZOJ1047-[HAOI2007]理想的正方形
    【单调队列优化DP】BZOJ1855-[Scoi2010]股票交易
    [Usaco2008 Open]Word Power 名字的能量
    bzoj 1623: [Usaco2008 Open]Cow Cars 奶牛飞车
    bzoj 3479: [Usaco2014 Mar]Watering the Fields
    1163: [Baltic2008]Mafia
    [HAOI2007]反素数ant
  • 原文地址:https://www.cnblogs.com/CH520/p/9452300.html
Copyright © 2011-2022 走看看