NSString之Format

三点用法总结:

  1. 类型转换:基本类型到对象类型
    1. 需要调用NSString的fotmat方法
    2. NSString  *location = [NSString  stringWithFormat:@"location = %i ,2];
  2. 连续定义
    1. 字符串接字符串,或者基本类型的转换(红色的下面是橙色)
    2. NSString *color = [NSString stringWithFormat:@"Red under is %@,location = %i",@"Orange",2];

   3.  通过占位符为变量名进行占位,对变量名内的数据进行格式化

      1.NSString *str = @"color"; unichar ch = [str characterAtIndex:3]; NSLog(@"index:%c",ch);

      2. 使用可变对象一定要先初始化(NSMutableString可以直接使用Format,添加也可以使用Format进行格式化)

        1.NSMutableString * str_M = [NSMutableString  stringWithFormat:@""];

        2.%@:对对象类型数据进行占位,即使这个数据是存储于变量中的对象

                     3.[str_M appendFormat:@"%@=%@", str, dic[str]];//把字典的key和value连接起来

  

// 不但可以通过占位符来对字符串进行连续定义

        unichar ch = 'Y';

        NSLog(@"%@", [NSString stringWithFormat:@"%c", ch]); //还可以对变量内数据进行Format操作

        color = [NSString stringWithFormat:@"Red under is %@,location = %d",@"Orange",2];

        NSLog(@"占位符自定义:%@",color);

原文地址:https://www.cnblogs.com/pruple/p/5235932.html