`
dcj3sjt126com
  • 浏览: 1876831 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用NSString 的格式化大全

    博客分类:
  • IOS
阅读更多

格式定义
The format specifiers supported by the NSString formatting methods and CFString formatting functions follow the IEEE printf specification; the specifiers are summarized in Table 1. Note that you can also use the “n$” positional specifiers such as %1$@ %2$s. For more details, see the IEEE printf specification. You can also use these format specifiers with the NSLog function.

Table 1 Format specifiers supported by the NSString formatting methods and CFString formatting functions
定义 说明
%@ Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function.
%% ‘%’ character
%d, %D, %i Signed 32-bit integer (int)
%u, %U Unsigned 32-bit integer (unsigned int)
%hi Signed 16-bit integer (short)
%hu Unsigned 16-bit integer (unsigned short)
%qi Signed 64-bit integer (long long)
%qu Unsigned 64-bit integer (unsigned long long)
%x Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and lowercase a–f
%X Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and uppercase A–F
%qx Unsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and lowercase a–f
%qX Unsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and uppercase A–F
%o, %O Unsigned 32-bit integer (unsigned int), printed in octal
%f 64-bit floating-point number (double)
%e 64-bit floating-point number (double), printed in scientific notation using a lowercase e to introduce the exponent
%E 64-bit floating-point number (double), printed in scientific notation using an uppercase E to introduce the exponent
%g 64-bit floating-point number (double), printed in the style of %e if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise
%G 64-bit floating-point number (double), printed in the style of %E if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise
%c 8-bit unsigned character (unsigned char), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit
%C 16-bit Unicode character (unichar), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit
%s Null-terminated array of 8-bit unsigned characters. %s interprets its input in the system encoding rather than, for example, UTF-8.
%S Null-terminated array of 16-bit Unicode characters
%p Void pointer (void *), printed in hexadecimal with the digits 0–9 and lowercase a–f, with a leading 0x
%L Length modifier specifying that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument
%a 64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a lowercase p to introduce the exponent
%A 64-bit floating-point number (double), printed in scientific notation with a leading 0X and one hexadecimal digit before the decimal point using a uppercase P to introduce the exponent
%F 64-bit floating-point number (double), printed in decimal notation
%z Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument
%t Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer type argument
%j Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a intmax_t or uintmax_t argument

平台依赖
Mac OS X uses several data types—NSInteger, NSUInteger,CGFloat, and CFIndex—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively. In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned long, respectively. To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 2. Note that in some cases you may have to cast the value.

Table 2 Format specifiers for data types
类型 定义 建议
NSInteger %ld or %lx Cast the value to long
NSUInteger %lu or %lx Cast the value to unsigned long
CGFloat %f or %g %f works for floats and doubles when formatting; but see below warning when scanning
CFIndex %ld or %lx The same as NSInteger
pointer %p %p adds 0x to the beginning of the output. If you don’t want that, use %lx and cast to long.
long long %lld or %llx long long is 64-bit on both 32- and 64-bit platforms
unsigned long long %llu or %llx unsigned long long is 64-bit on both 32- and 64-bit platforms

The following example illustrates the use of %ld to format an NSInteger and the use of a cast.

1
2
NSInteger i = 42;
printf("%ld\n"(long)i);

In addition to the considerations mentioned in Table 2, there is one extra case with scanning: you must distinguish the types for float and double. You should use %f for float, %lf for double. If you need to use scanf (or a variant thereof) with CGFloat, switch to double instead, and copy the double to CGFloat.

1
2
3
4
CGFloat imageWidth;
double tmp;
sscanf (str, "%lf"&tmp);
imageWidth = tmp;

It is important to remember that %lf does not represent CGFloat correctly on either 32- or 64-bit platforms. This is unlike %ld, which works for long in all cases.

分享到:
评论

相关推荐

    iOS NSString详细操作

    - `stringWithFormat:`方法可以用来创建格式化的字符串。 ```objective-c int textCount = textField.text.length; NSString *aString = [NSString stringWithFormat:@"你输入的字符串长度是:%d", textCount]; ...

    iOS日期格式化类(刚刚、一分钟前、昨天、周几)

    在iOS开发中,日期和时间的格式化是一个常见的任务,特别是在显示用户友好的时间戳时。"iOS日期格式化类(刚刚、一分钟前、昨天、周几)"这个主题聚焦于如何利用Objective-C来处理日期的本地化显示,使得时间表述更加...

    NSString+Extended

    这个扩展可能包含了对字符串的各种实用操作,比如格式化、拼接、查找、替换等。现在我们来详细探讨一下这个扩展可能包含的一些知识点。 1. **字符串格式化**: 类别中可能包含了类似`stringWithFormat:arguments:`...

    NSString扩展类

    1. **字符串格式化**:例如,可能会有`stringWithFormat:args:`的快捷方法,方便开发者快速创建格式化的字符串。 2. **字符串拼接**:提供如`concatenateString:`或`appendString:`的方法,使得多个字符串合并变得...

    NSString扩展

    该文件通过宏定义提供了一些简单的字符串格式化方法,这些方法可以帮助开发者快速地将数值类型转换成字符串。 - `NSStringFromInt`: 将整型数字转换为字符串。 - `NSStringFromFloat`: 将浮点型数字转换为字符串。 -...

    Objective-c中的字符串格式化输出

    Objective-C提供了类似于C语言的printf函数的字符串格式化方法,但它是通过`NSString`类的方法和`CFString`格式化函数实现的。这些方法支持一系列的格式规范符,使得输出的字符串可以包含变量值、对象描述等信息。 ...

    OC实现的电话号码格式化

    本话题将深入探讨如何使用OC来实现电话号码的格式化,即去除电话号码中的特殊字符,如破折号(-)、空格( )等非数字字符,使其呈现出标准、易读的形式。 电话号码的格式化是用户体验设计中的一个重要环节,因为...

    NSString用法.pdf

    - `stringWithFormat:`方法允许我们根据指定的格式创建字符串,它可以接受参数并将其插入到格式化字符串中,如`NSString* str5 = [NSString stringWithFormat:@"Now is: %@", someDate];`。 5. **其他方法**: - ...

    iphone 开发学习基础NSString

    大小写转换在处理用户输入或格式化显示时很常见。可以使用`uppercaseString`和`lowercaseString`方法进行转换,如`NSString *upperStr = [str uppercaseString];`和`NSString *lowerStr = [str lowercaseString];` ...

    Xcode插件:将JSON格式化输出为模型的属性ESJsonFormat-Xcode.zip

    JSON中的key对应的value为Null的话会格式化成NSString类型 格式化之前光标放在你需要添加属性的地方 RootClass需要自己手动创建,插件只负责RootClass里面的属性生成 ...

    NSString处理时间里面月份含零

    总之,通过使用`NSString`和`NSDate`,结合`NSDateFormatter`的格式化能力,我们可以轻松处理包含月份的日期字符串,确保月份始终以两位数的形式呈现。这个过程不仅适用于“月份含零”的问题,还适用于其他日期和...

    Objective-C中字符串NSString的常用操作方法总结

    1.创建字符串 (1)常量字符串 代码如下: NSString *string = @”i am an iOSDevTip!”; (2)常用创建方法 代码如下: NSString *string = [[NSString alloc] ...2.格式化创建字符串 (1)int格式化字符串 代码如下

    一句代码 实现银行卡手机号输入时格式化

    在iOS开发中,为了提升用户体验,我们经常需要对用户输入的数据进行格式化处理,比如银行卡号和手机号的输入。在给定的标题“一句代码 实现银行卡手机号输入时格式化”中,我们可以理解到,有一种高效的方法可以实现...

    ios demo,NSData和NSString之间的相互转换的源代码

    NSData通常用于存储二进制数据,如图片、音频或任何其他非文本格式的数据,而NSString则专门用于处理文本内容。在实际应用中,我们经常需要在这两种类型之间进行转换,以便进行各种操作。这个“ios demo,NSData和...

    NSStringDemo

    在Objective-C编程中,`NSString` 和 `...这包括字符串的初始化、比较、格式化、拼接、分割、查找和替换等常见操作。通过学习这些示例,开发者可以更熟练地掌握Objective-C中的字符串处理技术,提升代码的质量和效率。

    MGFormatter:在iOS上运行的JSON字符串格式化程序

    MGFormatter MGFormatter可以使用自定义的关键字颜色和字体在视图中格式化JSON或HTML代码。例子要运行示例项目,请克隆存储库,然后首先从Example目录运行pod install 。要求iOS 8.0以上Xcode 9.0以上用法只需创建一...

    NSData 与 NSString,Byte数组,UIImage 的相互转换

    - 更常见的是,如果数据代表ASCII或UTF-8编码的文本,可以使用`+[NSString stringWithData:encoding:]`将`NSData`转换为`NSString`。指定正确的编码(如NSUTF8StringEncoding)非常重要,以确保正确解析文本。 2. ...

    NSString+ToolString

    在实际开发中,`NSString+ToolString` 类可能还会包含其他实用功能,比如字符串的加密解密、格式化、去除空白字符等。这类工具类的一个优点是提高了代码的可读性和复用性,避免了在多个地方重复编写相同的验证逻辑。...

    iOS中金额字符串格式化显示的方法示例

    其实,苹果提供了 NSNumberFormatter 用来处理金额字符串格式化显示,可以使用NSNumberFormatter来实现金额字符串格式化显示,这样可以避免我们上述实现的繁琐代码。 iOS中金额字符串格式化显示是一个常见的问题,...

Global site tag (gtag.js) - Google Analytics