Objective-C 的字串分為不可變與可變兩類,日常開發中幾乎所有資料處理都離不開它們。以下把常用操作整理成一份可隨時回頭查的清單。
1、不可變字串 NSString:不能修改物件內容,但可以改變物件指向的指標。
2、可變字串 NSMutableString:可以直接修改物件內容。
// 1. 直接將字串常量賦值給引用 NSString *str1 = @"hello"; NSLog(@"str1=%@", str1); // 2. initWithString:由既有字串物件建立 NSString *str2 = [[NSString alloc] initWithString:str1]; // 3. initWithUTF8String:把 C 字串轉為 OC 字串 NSString *str3 = [[NSString alloc] initWithUTF8String:"iOS"]; // 4. initWithFormat:以格式化字串建立 int age = 20; NSString *str4 = [[NSString alloc] initWithFormat:@"name is %@, age is %d", str1, age];
NSUInteger len = str1.length; NSString *str1 = @"hello"; unichar c = [str1 characterAtIndex:0]; // 結果為 h
compare: 會區分大小寫,回傳 NSComparisonResult 列舉值:
· NSOrderedAscending:前者小於後者
· NSOrderedSame:兩者相等
· NSOrderedDescending:前者大於後者
NSString *str1 = @"hello";
NSString *str3 = [[NSString alloc] initWithUTF8String:"iOS"];
NSComparisonResult cmp = [str1 compare:str3];
if (cmp == NSOrderedAscending) {
NSLog(@"%@<%@", str1, str3);
} else if (cmp == NSOrderedSame) {
NSLog(@"%@=%@", str1, str3);
} else if (cmp == NSOrderedDescending) {
NSLog(@"%@>%@", str1, str3);
}
// 結果:hello<iOS
不區分大小寫的比較,或需要更細緻的比較選項時使用:
· NSNumericSearch:字串中含數字時,依數字大小比較(ios5 < ios12)
· NSCaseInsensitiveSearch:不區分大小寫(iOS7 = ios7)
NSComparisonResult cmp = [str1 caseInsensitiveCompare:str3]; str1 = @"iOS7"; str3 = @"ios7"; cmp = [str1 compare:str3 options:NSCaseInsensitiveSearch]; // 結果:iOS7=ios7
NSString *str1 = @"hello";
NSString *str2 = [[NSString alloc] initWithString:str1];
if ([str1 isEqualToString:str2]) {
NSLog(@"%@ equal %@", str1, str2);
}
// 結果:hello equal hello
NSString *str1 = @"hello"; NSString *str6 = [str1 uppercaseString]; // HELLO NSString *str7 = [str6 lowercaseString]; // hello
NSString *str1 = @"www.baidu.com";
if ([str1 hasPrefix:@"www"]) { NSLog(@"yes"); } // yes
if ([str1 hasSuffix:@"com"]) { NSLog(@"yes"); } // yes
// 從某個索引取到結尾
NSString *str1 = @"This is string A";
NSString *res = [str1 substringFromIndex:1]; // his is string A
// 從開頭取到某個索引(不含該索引位置的字元)
NSString *res = [str1 substringToIndex:2]; // Th
// 巢狀使用
[[str1 substringFromIndex:8] substringToIndex:5]; // strin
// 依範圍擷取
NSRange range = {8, 5};
range = NSMakeRange(8, 5); // 以函式產生範圍
NSString *res = [str1 substringWithRange:range]; // strin
NSString *str1 = @"This is string A";
NSRange range = [str1 rangeOfString:@"string"]; // {8,6}
if (range.location != NSNotFound) {
NSLog(@"find");
}
NSString *str = @"This is string B"; NSMutableString *mStr = [[NSMutableString alloc] initWithString:str];
NSMutableString *mStr = [[NSMutableString alloc] initWithString:@"This is string B"]; [mStr insertString:@"hello " atIndex:0]; // 結果:hello This is string B
[mStr appendString:@" shanghai"]; // 結果:This is string B shanghai
[mStr appendFormat:@" %d", 20]; // 結果:This is string B 20
[mStr deleteCharactersInRange:NSMakeRange(0, 6)]; // 結果:s string B
NSMutableString *mStr = [[NSMutableString alloc] initWithString:@"This is string B"];
[mStr replaceCharactersInRange:NSMakeRange(2, 2) withString:@"IS"];
// 結果:ThIS is string B
// 將字串中所有的 is 都替換為 IS
NSRange range = [mStr rangeOfString:@"is"];
while (range.location != NSNotFound) {
[mStr replaceCharactersInRange:range withString:@"IS"];
range = [mStr rangeOfString:@"is"];
}
NSMutableString *mStr = [[NSMutableString alloc] initWithString:@"This is string B"];
[mStr replaceOccurrencesOfString:@"is"
withString:@"IS"
options:1
range:NSMakeRange(0, mStr.length)];
// 結果:ThIS IS string B
[mStr setString:@"hello"]; // 結果:hello
在迴圈中反覆修改字串時,務必使用 NSMutableString 並搭配 replaceOccurrencesOfString:,而不是用 NSString 的 stringByReplacingOccurrencesOfString: 反覆產生新物件,兩者在效能上差距明顯。另外,rangeOfString: 找不到結果時會回傳 location 為 NSNotFound 的範圍,使用迴圈替換時一定要以此作為終止條件,否則容易陷入無窮迴圈。