
- 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 相關方法的完整列表。