码迷,mamicode.com
首页 > 其他好文 > 详细

字符串与基本数据类型转换

时间:2017-03-26 21:34:35      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:ons   with   span   logs   ring   nbsp   文字   length   font   

1.

  • - (NSUInteger)length;

    • 返回字符串的长度(有多少个文字)
  • - (unichar)characterAtIndex:(NSUInteger)index;

    • 返回index位置对应的字符

2.字符串和其他数据类型转换

  • 转为基本数据类型
    • - (double)doubleValue;
    • - (float)floatValue;
    • - (int)intValue;
    NSString *str1 = @"110";
    NSString *str2 = @"10";
    int res = str1.intValue + str2.intValue;
    NSLog(@"res = %i", res);
    NSString *str1 = @"110";
    NSString *str2 = @"10.1";
    double res = str1.doubleValue + str2.doubleValue;
    NSLog(@"res = %f", res);
  • 转为C语言中的字符串

  - (char *)UTF8String;

   NSString *str = @"abc";
    const char *cStr = [str UTF8String];
    NSLog(@"cStr = %s", cStr);
    char *cStr = "abc";
    NSString *str = [NSString stringWithUTF8String:cStr];
    NSLog(@"str = %@", str);

 

字符串与基本数据类型转换

标签:ons   with   span   logs   ring   nbsp   文字   length   font   

原文地址:http://www.cnblogs.com/xufengyuan/p/6623909.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!