`
hxg_2009
  • 浏览: 4635 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
社区版块
存档分类
最新评论

关于stringWithFormat:

    博客分类:
  • IOS
 
阅读更多
在ObjectiveC中NSString中有一个 stringWithFormat:方法

常见的输出方式:
NSString *height;
height = [NSString stringWithFormat:@"Your height is %d feet, %d inches.",5,11];
NSLog(@"%@",height);

输出结果:
2013-04-12 10:30:47.742 String[2161:303] Hello, World!
2013-04-12 10:30:47.744 String[2161:303] Your height is 5 feet, 11 inches.


输出多个字符的方式(以两个字符为例):

NSString *str;
NSString *str1 = @"123";
NSString *str2 = @"465";
str = [NSString stringWithFormat:@"%@,%@",str1, str2];
NSLog(@"%@",str);

输出结果:

2013-04-12 10:31:48.211 String[2171:303] Hello, World!
2013-04-12 10:31:48.213 String[2171:303] 123,465

下面这种方式是错误的:

str = [NSString stringWithFormat:@"123",@"456"];
NSLog(@"%@",str);

警告信息:
关于stringWithFormat:
输出结果:

2013-04-12 10:35:34.041 String[2209:303] Hello, World!
2013-04-12 10:35:34.043 String[2209:303] 123
@“456” 不会被输出;

以上代码可以改写成:
str = [NSString stringWithFormat:@"%@,%@",@"123",@"456"];
NSLog(@"%@",str);

输出结果:

2013-04-12 10:42:39.539 String[2229:303] Hello, World!
2013-04-12 10:42:39.541 String[2229:303] 123,456
分享到:
评论

相关推荐

    OC增删改查

    NSMutableString *sr=[NSMutableString stringWithFormat:@"%@",@"sdfdgdfg"]; //增加 [sr appendFormat:@"ad"]; NSLog(@"%@",sr); //增加 [sr appendFormat:@"%@",@"ad"]; NSLog(@"%@",sr); //删除 [sr ...

    iOS CSV文件导出,保存

    [str appendString:[NSString stringWithFormat:@"%@,",key]]; } } //换行 [str appendString:@"\n"]; for (NSString *value in dict.allValues) { [str appendString:[NSString stringWithFormat:@"%@...

    ios WKWebView 的使用和交互

    // NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"file://%@",path] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]] relativeToURL:...

    objective-c小技巧

    直接调用成员变量并且给其赋值:member=[NSString stringWithFormat:@””];将不执行setmember 方法。 使用self调用成员变量并且给其赋值:self.member=[NSString stringWithFormat:@””];将执行setmember方法...

    AutoreleasePool:自动释放池

    // 场景 1 NSString *string = [ NSString stringWithFormat: @" leichunfeng " ]; string_weak_ = string; // 场景 2// @autoreleasepool {// NSString *string = [NSString stringWithFormat:@"leichunfeng"];// ...

    FormatterKit.zip

    FormatterKit.zip,`stringWithFormat:` for the sophisticated hacker set

    ios-WebView实现网页图片获取放大.zip

    NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('%@').length", tag]; int len = [[self stringByEvaluatingJavaScriptFromString:jsString] intValue]; return len; } ...

    ios-基于Reachability扩展的2G3G4G 网络检测,支持block回调.zip

    self.changeLable.text =[NSString stringWithFormat:@"当前无网络 %@",@(status)]; } NSLog(@"current net status = %@",@(status)); }]; github : ...

    ios-tableView实现三级地市.zip

    代码简洁,几句话就可以实现。... weakSelf.valueLab.text = [NSString stringWithFormat:@"%@-%@-%@-%@", provice, city, area, code]; }; [self presentViewController:alertVc animated:YES completion:nil];

    ios-MCAlart.zip

    [self AlerViewNSNotification:[NSString stringWithFormat:@"%@",data]]; }]; for (int i = 0; i ; i ) { alart.mcLayoutKeyBourd(i); } // alart.mcLayoutKeyBourd(0).mcLayoutKeyBourd(1)....

    iOS base64 加密解密 通用类实例代码

    NSString *str = [NSString stringWithFormat:@YWE=]; NSString *str1 = [NSString stringWithFormat:@aa]; NSLog(@resultStr========%@,[CommonFunc textFromBase64String:str]); //使用类名进行调用 NSLog(@...

    ios学习笔记之基础数据类型的转换

    NSString *string = [NSString stringWithFormat:@%@%@,对象,对象]; 字符串与int int intString = [newString intValue]; NSString *string = [NSString stringWithFormat:@%d,intSteing]; 字符串与float float ...

    ios-JAScrollChartView.zip

    self.chartView = [[JAScrollChartView alloc] initWithFrame:...unitData.lableTitle = [NSString stringWithFormat:@"day %d", (int)i]; [data addObject:unitData]; } [self.chartView addChartData:data];

    不会说话的汤姆猫

    NSString *fileName=[NSString stringWithFormat:format,i]; UIImage* image=[UIImage imageNamed:fileName]; [imageList addObject:image]; } [_tomcatImageView setAnimationImages:imageList]; [_...

    ios-LLXAlertPop.zip

    //样式一(更多样式请下载demo) ... lab.text = [NSString stringWithFormat:@"%@,下标%ld",button.currentTitle,(long)didRow]; }]; 如果对您有用请右上角点个 star,谢谢咯~ 如发现bug请lssues我~

    LFLineChart图标

    self.lineChart.yMarkTitles = @[@"0",[NSString stringWithFormat:@"%.2lf",max/5],[NSString stringWithFormat:@"%.2lf",max*2/5],[NSString stringWithFormat:@"%.2lf",max*3/5],[NSString stringWithFormat:@"%....

    ios开发的简单计算器

    ios 委托 简单计算器 - (IBAction)Result:(UIButton *)sender { NSString *string; int a = self.i; string = [[NSString alloc]... self.Number.text = [NSString stringWithFormat:@"%2f",self.i]; }

    01-加法计算器

    self.resultLabel.text = [NSString stringWithFormat:@"%ld", (long)result]; // 退出键盘 // [self.num1TextField resignFirstResponder]; // [self.num2TextField resignFirstResponder]; // 只要是自己或自己...

    9*9乘法口诀表

    9*9乘法口诀表 label.text = [NSString stringWithFormat:@"%d*%d=%d",i,j,i*j];

    Object-C字符串

    NSString *str2 = [NSString stringWithFormat:@"The value is %d", 42]; // 使用格式化字符串创建字符串 获取字符串长度: NSString *str = @"Objective-C"; NSUInteger length = [str length]; // 获取字符串长度 ...

Global site tag (gtag.js) - Google Analytics