Objc-C 知識點回顧 四 NSString 、NSMutableString
1.NSString
//NSString:不可變字符串, 一旦創建, 不能修改
//初始化方法
//創建空字符串
NSString *string1 = [[NSString alloc] init];
NSLog(@"%@", string1);
//根據給定的常量字符串,創建NSString對象
NSString *string2 = [[NSString alloc] initWithString:@"iPhone"];
NSLog(@"%@", string2);
//常量區字符串
NSString *string3 = @"iPhone";
NSLog(@"%@", string3);
NSLog(@"%p", string3);//輸出地址
//如果要創建的字符串中出現其他變量, 需要用initWithFormat的方式創建
NSInteger version = 6;
NSString *string4 = [[NSString alloc] initWithFormat:@"iPhone %ld", version];
NSLog(@"%@", string4);
//便利構造器
NSString *str1 = [NSString string];
NSLog(@"%@", str1);
NSString *str2 = [NSString stringWithString:@"iOS"];
NSLog(@"%@", str2);
NSString *str3 = @"iOS";
NSLog(@"%@", str3);
NSString *str4 = [NSString stringWithFormat:@"iOS %ld", version];
NSLog(@"%@", str4);
//獲取字符串的長度
// NSUInteger length = [str4 length];//調用getter方法
// NSLog(@"%lu", length);
NSLog(@"%lu", str4.length);//點語法
//判斷字符串是否以置頂字符串開始或結束
NSString *str5 = @"abcdpp123";//常量區字符串
NSString *str6 = [[NSString alloc] initWithFormat:@"abcdpp123"];//堆區字符串
//前綴
BOOL result = [str6 hasPrefix:@"abd"];
NSLog(@"%d", result);
//後綴
result = [str6 hasSuffix:@"23"];
NSLog(@"%d", result);
//字符串截取(截取子字符串)
//獲取abc(開頭字符串)
NSString *subStr1 = [str6 substringToIndex:3];
NSLog(@"%@", subStr1);
//獲取123(結尾字符串)
NSString *subStr2 = [str6 substringFromIndex:6];
NSLog(@"%@", subStr2);
//獲取dpp(中間字符串)
//NSRange range = {3, 3};
NSString *subStr3 = [str6 substringWithRange:NSMakeRange(3, 3)];
NSLog(@"%@", subStr3);
//拼接字符串
NSString *newSubStr = [subStr1 stringByAppendingString:subStr2];
NSLog(@"%@", subStr1);
NSLog(@"%@", subStr2);
NSLog(@"%@", newSubStr);
NSLog(@"%p", newSubStr);//輸出地址
NSString *newSubStr1 = [NSString stringWithFormat:@"%@%@", subStr1, subStr2];
NSLog(@"%@", newSubStr1);
NSLog(@"%p", newSubStr1);//輸出地址
//替換字符串
//@"NBA" -> @"CBA";
NSString *str7 = @"NBANNN";
NSString *newSubStr2 = [str7 stringByReplacingOccurrencesOfString:@"N" withString:@"C"];
NSLog(@"%@", newSubStr2)
//字符串比較
//@"NBA" 和 @"CBA";
// NSString *str8 = @"NBA";
// NSString *str9 = @"CBA";
NSString *str8 = [[NSString alloc] initWithFormat:@"NBA123456aa"];
NSString *str9 = [[NSString alloc] initWithFormat:@"NBA123456aa"];
//isEqualToString比較的是字符串的內容
if ([str8 isEqualToString:str9]) {
NSLog(@"字符串相等");
} else {
NSLog(@"不一致");
}
//==比較兩個對象是否一致, 如果對象一致, 他們的內容一定一致
if (str8 == str9) {
NSLog(@"字符串相等");
} else {
NSLog(@"不一致");
}
//字符串和數值類型相互轉換
//@"123"
//轉化成數字, 遇到非數字即停止轉換
NSString *str10 = @"123a456";
int a = [str10 intValue];
NSLog(@"%d", a);
NSInteger b = [str10 integerValue];
NSLog(@"%ld", b);
//大小寫轉換操作
//@"ios" -> @"iOS"
NSString *str11 = @"ios";
//全部大寫
NSString *newStr = [str11 uppercaseString];
NSLog(@"%@", newStr);
//首字母大寫
NSLog(@"%@", [str11 capitalizedString]);
//全部小寫
NSLog(@"%@", [str11 lowercaseString]);
2.NSMutableString:可變字符串
//@"iPhone"
//創建可變字符串, 容器大小是一個預期值, 一般寫為0, 讓系統自己根據內容大小分配空間
NSMutableString *mString = [[NSMutableString alloc] initWithCapacity:0];
NSLog(@"%@", mString);
//添加字符串
[mString appendString:@"iPhone"];
NSLog(@"%@", mString);
[mString appendFormat:@"iPhone %ld", version];;
NSLog(@"%@", mString);
//插入字符串
[mString insertString:@" " atIndex:6];
NSLog(@"%@", mString);
//刪除字符串
[mString deleteCharactersInRange:NSMakeRange(0, 7)];
NSLog(@"%@", mString);
//做個練習,給定一個圖片文件名,判斷字符串中是否以“png”結尾,如果是就替換成“jpg”,如果不是,就拼接”.jpg”。
NSMutableString *name = [[NSMutableString alloc] initWithCapacity:0];
[name appendString:@"藍鷗.png"];
if ([name hasPrefix:@"png"]) {
NSLog(@"以'png'結尾");
[name replaceCharactersInRange:NSMakeRange(name.length - 3, 3) withString:@"jpg"];
} else {
NSLog(@"不以'png'結尾");
[name appendString:@".jpg"];
}
NSLog(@"%@", name);