NSString中stringWithFormat的用法

时间:2021-11-28 23:17:55

#import <Foundation/Foundation.h>


int main(){

   NSString* str;

    str = [NSStringstringWithFormat:@"your height is %d feet, %d inches.",5,11];

//    NSLog(@"%@",str);

   NSLog(@"%@",str);

    

   NSString* str1 ;

    str1 =@"MAC";

    //指定NSString的实例来创建字符串

   NSString* str2 ;

    str2 = [NSStringstringWithFormat:@"%@ PRO",str1];

   NSLog(@"%@",str2);

    

    //指定整数,创建字符串

   int i = 80;

   NSString* str3;

    str3 = [NSStringstringWithFormat:@"%d %%",i];

    NSLog(@"%@",str3);//这样str3就变为80%

    

   NSString* str4;

    str4 = [NSStringstringWithFormat:@"0x%x",i];

   NSLog(@"%@",str4);

    

   return 0;

}