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

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);
原文地址:https://www.cnblogs.com/xufengyuan/p/6623909.html