- Objective-C 基础
- Objective-C - 首页
- Objective-C - 概述
- Objective-C - 环境设置
- Objective-C - 程序结构
- Objective-C - 基本语法
- Objective-C - 数据类型
- Objective-C - 变量
- Objective-C - 常量
- Objective-C - 运算符
- Objective-C - 循环
- Objective-C - 决策
- Objective-C - 函数
- Objective-C - 块
- Objective-C - 数字
- Objective-C - 数组
- Objective-C - 指针
- Objective-C - 字符串
- Objective-C - 结构体
- Objective-C - 预处理器
- Objective-C - Typedef
- Objective-C - 类型转换
- Objective-C - 日志处理
- Objective-C - 错误处理
- 命令行参数
- 高级 Objective-C
- Objective-C - 类与对象
- Objective-C - 继承
- Objective-C - 多态
- Objective-C - 数据封装
- Objective-C - 分类
- Objective-C - 模拟
- Objective-C - 扩展
- Objective-C - 协议
- Objective-C - 动态绑定
- Objective-C - 复合对象
- Obj-C - Foundation 框架
- Objective-C - 快速枚举
- Obj-C - 内存管理
- Objective-C 有用资源
- Objective-C - 快速指南
- Objective-C - 有用资源
- Objective-C - 讨论
Objective-C 字符串
在 Objective-C 编程语言中,字符串使用 NSString 表示,其子类 NSMutableString 提供了几种创建字符串对象的方式。创建字符串对象最简单的方法是使用 Objective-C 的 @"..." 结构。
NSString *greeting = @"Hello";
下面是一个创建和打印字符串的简单示例。
#import <Foundation/Foundation.h>
int main () {
NSString *greeting = @"Hello";
NSLog(@"Greeting message: %@\n", greeting );
return 0;
}
编译并执行上述代码后,将产生如下结果:
2013-09-11 01:21:39.922 demo[23926] Greeting message: Hello
Objective-C 支持广泛的方法来操作字符串:
| 序号 | 方法及用途 |
|---|---|
| 1 | - (NSString *)capitalizedString; 返回接收者的首字母大写表示形式。 |
| 2 | - (unichar)characterAtIndex:(NSUInteger)index; 返回给定数组位置处的字符。 |
| 3 | - (double)doubleValue; 将接收者的文本作为双精度浮点数返回其浮点值。 |
| 4 | - (float)floatValue; 将接收者的文本作为单精度浮点数返回其浮点值。 |
| 5 | - (BOOL)hasPrefix:(NSString *)aString; 返回一个布尔值,指示给定字符串是否与接收者的开头字符匹配。 |
| 6 | - (BOOL)hasSuffix:(NSString *)aString; 返回一个布尔值,指示给定字符串是否与接收者的结尾字符匹配。 |
| 7 | - (id)initWithFormat:(NSString *)format ...; 返回一个 NSString 对象,该对象使用给定的格式字符串作为模板进行初始化,并将剩余的参数值替换到其中。 |
| 8 | - (NSInteger)integerValue; 返回接收者文本的 NSInteger 值。 |
| 9 | - (BOOL)isEqualToString:(NSString *)aString; 返回一个布尔值,指示给定字符串是否使用基于 Unicode 的逐字比较等于接收者。 |
| 10 | - (NSUInteger)length; 返回接收者中 Unicode 字符的数量。 |
| 11 | - (NSString *)lowercaseString; 返回接收者的全部小写表示形式。 |
| 12 | - (NSRange)rangeOfString:(NSString *)aString; 查找并返回接收者中给定字符串第一次出现的范围。 |
| 13 | - (NSString *)stringByAppendingFormat:(NSString *)format ...; 返回一个字符串,该字符串通过将使用给定格式字符串和后续参数构建的字符串附加到接收者来创建。 |
| 14 | - (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set; 返回一个新的字符串,该字符串通过从接收者的两端删除给定字符集中包含的字符来创建。 |
| 15 | - (NSString *)substringFromIndex:(NSUInteger)anIndex; 返回一个新字符串,其中包含从给定索引处的字符到接收者末尾的字符。 |
以下示例使用了上面提到的几个函数:
#import <Foundation/Foundation.h>
int main () {
NSString *str1 = @"Hello";
NSString *str2 = @"World";
NSString *str3;
int len ;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
/* uppercase string */
str3 = [str2 uppercaseString];
NSLog(@"Uppercase String : %@\n", str3 );
/* concatenates str1 and str2 */
str3 = [str1 stringByAppendingFormat:@"World"];
NSLog(@"Concatenated string: %@\n", str3 );
/* total length of str3 after concatenation */
len = [str3 length];
NSLog(@"Length of Str3 : %d\n", len );
/* InitWithFormat */
str3 = [[NSString alloc] initWithFormat:@"%@ %@",str1,str2];
NSLog(@"Using initWithFormat: %@\n", str3 );
[pool drain];
return 0;
}
编译并执行上述代码后,将产生如下结果:
2013-09-11 01:15:45.069 demo[30378] Uppercase String : WORLD 2013-09-11 01:15:45.070 demo[30378] Concatenated string: HelloWorld 2013-09-11 01:15:45.070 demo[30378] Length of Str3 : 10 2013-09-11 01:15:45.070 demo[30378] Using initWithFormat: Hello World
您可以在NSString 类参考中找到 Objective-C NSString 相关方法的完整列表。