</pre><p></p><p></p><pre name="code" class="objc">//
// main.m
// NSString
//
// Created by LiuWei on 15/4/14.
// Copyright (c) 2015年 LiuWei. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
// NSString 创建一个字符串对象, NSString字符串都以@开头
NSString *str = @"黑马程序员";
NSLog(@"我是%@", str); // 在格式化字符串中NSString字符串以%@格式接收
// NSString字符串格式化生成方式 使用类方法 stringWithFormat
NSString *str2 = [NSString stringWithFormat:@"我的年龄是 %i, 我的性别是%@", 30, @"男"];
NSLog(@"%@", str2);
// NSString 对象方法 length 返回字符串中字符的个数
NSString *str3 = @"黑马程序员";
NSLog(@"字符串 ”%@“ 中有 %ld 个字符", str3, [str3 length]);
return 0;
}
查看官方文档
NSString
The NSString class declares the programmatic interface for an object that manages immutable strings. An immutable string is a text string that is defined when it is created and subsequently cannot be changed. NSString is implemented to represent an array of Unicode characters, in other words, a text string.
NSString类给对象管理不可变字符串声明了一个程序化的接口。 一个不可变的文本字符串就是定义并创建之后不可再修改。NSString是通过一个Unicode字符数组来表现的,换句话说,就是一个文本字符串。
The mutable subclass of NSString isNSMutableString. NSString的一个可变子类就是NSMutableString.
The NSString class has two primitive methods—length andcharacterAtIndex:—that provide the basis for all other methods in its interface. Thelength method returns the total number of Unicode characters in the string.characterAtIndex: gives access to each character in the string by index, with index values starting at0.
NSString类有两个基本的方法-----length和characterAtIndex: 为其它方法提供了最基本的接口。length方法返回一个字符串中所有Unicode字符的个数。
characterAtIndex:通过索引可以访问字符串中的任一字符,索引以0开始。
- initWithFormat:
Returns an NSString object initialized by using a given format string as a template into which the remaining argument values are substituted.
通过所给定的格式模板和参数,返回一个初始化的NSString对象
Parameters 参数
| format 格式 | A format string. See Formatting String Objects for examples of how to use this method, andString Format Specifiers for a list of format specifiers. This value must not benil.
IMPORTANT重要 Raises an NSInvalidArgumentException ifformat isnil. 如果格式为nil则会引发一个NSInvalidArgumentException异常 |
| ... | A comma-separated list of arguments to substitute intoformat. 用来替换格式符号的,以逗号分隔的参数列表 |
Return Value返回值
An NSString object initialized by usingformat as a template into which the remaining argument values are substituted according to the system locale. The returned object may be different from the original receiver.
返回依据系统分配,通过格式化模板和参数值初始化的NSString对象。返回的对象可能与原始接收器不同
+ stringWithFormat:
Returns a string created by using a given format string as a template into which the remaining argument values are substituted.
通过所给定的格式模板和后续参数创建并返回一个字符串
Parameters
| format | A format string. See Formatting String Objects for examples of how to use this method, andString Format Specifiers for a list of format specifiers. This value must not benil.
IMPORTANT Raises an NSInvalidArgumentException ifformat isnil. |
| ... | A comma-separated list of arguments to substitute intoformat. |
Return Value
A string created by using format as a template into which the remaining argument values are substituted according to the system locale.
String Format Specifiers
Table 1 Format specifiers supported by theNSString formatting methods and CFString formatting functions
格式化指定符通常用来作为NSString的格式化方法和CFString的格式化函数
| Specifier 指定符 | Description描述 |
| %@ | Objective-C object, printed as the string returned bydescriptionWithLocale: if available, ordescription otherwise. Also works withCFTypeRef objects, returning the result of theCFCopyDescription function. OC对象,如果有效,打印通过descriptionWithLocale:返回的字符串,否则,输出其它描述。同样也可用于CFTypeRef对象。返回CFCopyDescription函数的结果 |
| %% | '%' character. ‘%’字符 |
| %d, %D | Signed 32-bit integer (int). 有符号32位整数 |
| %u, %U | Unsigned 32-bit integer (unsigned int).无符号32位整数 |
| %x | Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and lowercase a–f. 32位无符号整数,以0-9和小写字母a-f的形式打印16进制 |
| %X | Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and uppercase A–F.32位无符号整数,以0-9和大写字母A-F的形式打印16进制 |
| %o, %O | Unsigned 32-bit integer (unsigned int), printed in octal. 32位无符号整数,以8进制形式打印 |
| %f | 64-bit floating-point number (double). 64位符号点数 |
| %e | 64-bit floating-point number (double), printed in scientific notation using a lowercase e to introduce the exponent.64位符号点数,打印以小写字母e表达的科学记述法 |
| %E | 64-bit floating-point number (double), printed in scientific notation using an uppercase E to introduce the exponent.64位符号点数,打印以大写字母E表达的科学记述法 |
| %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. 64位符号点数,如果指数小于-4或者大于等于指定精度则以同%e的形式打印,否则以同%f的形式打印 |
| %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. 64位符号点数,如果指数小于-4或者大于等于指定精度则以同%E的形式打印,否则以同%f的形式打印 |
| %c | 8-bit unsigned character (unsigned char), printed byNSLog() as an ASCII character, or, if not an ASCII character, in the octal format\\ddd or the Unicode hexadecimal format\\udddd, whered is a digit. 8位无符号字符。参数如果是一个ASCII字符或者以8进制形式\\ddd 或都Unicode16进制\\udddd格式 NSLog()打印为一个ASCII字符。d表示一个数字 |
| %C | 16-bit Unicode character (unichar), printed byNSLog() as an ASCII character, or, if not an ASCII character, in the octal format\\ddd or the Unicode hexadecimal format\\udddd, whered is a digit. 16位Unicode字符。参数如果是一个ASCII字符或者以8进制形式\\ddd 或都Unicode16进制\\udddd格式 NSLog()打印为一个ASCII字符。d表示一个数字 |
| %s | Null-terminated array of 8-bit unsigned characters. Because the%s specifier causes the characters to be interpreted in the system default encoding, the results can be variable, especially with right-to-left languages. For example, with RTL,%s inserts direction markers when the characters are not strongly directional. For this reason, it’s best to avoid%s and specify encodings explicitly. 以0结尾的8位无符号字符数组。 |
| %S | Null-terminated array of 16-bit Unicode characters. 以0结尾的16位Unicode字符的数组 |
| %p | Void pointer (void *), printed in hexadecimal with the digits 0–9 and lowercase a–f, with a leading0x. 无类型指针, 以0x加上0-9和小写字母a-f组成的16进制数形式打印 |
| %a | 64-bit floating-point number (double), printed in scientific notation with a leading0x and one hexadecimal digit before the decimal point using a lowercasep to introduce the exponent. 64位浮点数。 |
| %A | 64-bit floating-point number (double), printed in scientific notation with a leading0X and one hexadecimal digit before the decimal point using a uppercaseP to introduce the exponent. |
| %F | 64-bit floating-point number (double), printed in decimal notation. |
Table 2 Length modifiers supported by theNSString formatting methods and CFString formatting functions
| Length modifier | Description |
| h | Length modifier specifying that a following d, o,u,x, orX conversion specifier applies to ashort orunsigned short argument. |
| hh | Length modifier specifying that a following d, o,u,x, orX conversion specifier applies to asigned char orunsigned char argument. |
| l | Length modifier specifying that a following d, o,u,x, orX conversion specifier applies to along orunsigned long argument. |
| ll, q | Length modifiers specifying that a following d, o,u,x, orX conversion specifier applies to along long orunsigned long long argument. |
| L | Length modifier specifying that a following a, A,e,E,f,F,g, orG conversion specifier applies to along double argument. |
| z | Length modifier specifying that a following d, o,u,x, orX conversion specifier applies to asize_t or the corresponding signed integer type argument. |
| t | Length modifier specifying that a following d, o,u,x, orX conversion specifier applies to aptrdiff_t or the corresponding unsigned integer type argument. |
| j | Length modifier specifying that a following d, o,u,x, orX conversion specifier applies to aintmax_t oruintmax_t argument. |
Platform Dependencies
OS X uses several data types—NSInteger,NSUInteger,CGFloat, andCFIndex—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment,NSInteger andNSUInteger are defined asint andunsigned int, respectively. In 64-bit environments,NSInteger andNSUInteger are defined aslong andunsigned 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 3. Note that in some cases you may have to cast the value.
Table 3 Format specifiers for data types
| Type | Format specifier | Considerations |
| 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 note the technique described below for scanning. |
| CFIndex | %ld or %lx | The same as NSInteger. |
| pointer | %p or %zx | %p adds 0x to the beginning of the output. If you don't want that, use%zx and no typecast. |
The following example illustrates the use of %ld to format an NSInteger and the use of a cast.
| NSInteger i = 42;
|
| printf("%ld\n", (long)i);
|
In addition to the considerations mentioned in Table 3, there is one extra case with scanning: you must distinguish the types forfloat anddouble. You should use%f for float,%lf for double. If you need to usescanf (or a variant thereof) withCGFloat, switch todouble instead, and copy thedouble toCGFloat.
| CGFloat imageWidth;
|
| double tmp;
|
| sscanf (str, "%lf", &tmp);
|
| imageWidth = tmp;
|
It is important to remember that %lf does not representCGFloat correctly on either 32- or 64-bit platforms. This is unlike%ld, which works for long in all cases.
本文深入解析了NSString类的功能和使用方法,包括字符串的创建、长度获取等基础操作,并详细介绍了NSString的格式化字符串方法,如initWithFormat和stringWithFormat,以及各种格式化指定符的应用。

933

被折叠的 条评论
为什么被折叠?



