查看文档(API) (NSString)
查看文檔(API)?
?
? ? ? ??OC的數據類型?
? ? ? ? NSInteger: 整型(32 = int, 64 = long)?
? ? ? ? CGFloat: 浮點型(32 = float, 64 = double)
? ? ? ??如何學習系統的類
? ? ? ? 1.查看文檔(API)
? ? ? ? a. 類的概況(overview)
? ? ? ? b. 類的繼承關系(inherit)
? ? ? ? c. 創建對象的方法(init, 自定義初始化方法, 便利構造器)
? ? ? ? d. 找方法? ?
? ? ? ? NSString: 不可變字符串類(字符串一旦被創建, 就不能更改), 繼承于NSObject
? ? ? ? 創建字符串對象
NSString *str1 = [NSString string];//空字符串NSLog(@"%@", str1); NSString *str2 = @"iOS";//常量字符串NSLog(@"%@", str2);NSLog(@"%p", str2);NSString *str4 = [[NSString alloc]initWithFormat:@"Hello, %@", str2];NSLog(@"%@", str4);NSInteger a = 10, b = 5;//創建一個字符串, "10 + 5 = 15"NSString *str5 = [[NSString alloc]initWithFormat:@"%ld + %ld = %ld", a, b, a + b ];NSLog(@"%@", str5);//創建一個字符串, "10 + 5 = 105"NSString *str6 = [[NSString alloc]initWithFormat:@"%ld + %ld = %ld%ld", a, b, a, b];NSLog(@"%@", str6);//創建一個字符串, "5 + 10 = 510"NSString *str7 = [NSString stringWithFormat:@"%ld + %ld = %ld%ld", b, a, b, a];NSLog(@"%@", str7);
求字符串的長度
NSString *str8 = @"你問我愛你有多深?月亮代表我的心";NSUInteger length = [str8 length];NSLog(@"%lu", length);獲取子字符串"我愛你"
//NSRange range = {2, 3};NSString *str9 = [str8 substringWithRange:range];NSRange range1 = NSMakeRange(9, 2);NSString *str9 = [str8 substringWithRange:range1];NSString *str9 = [str8 substringWithRange:NSMakeRange(9, 2)];NSLog(@"%@", str9); //"月亮代表我的心"NSString *str10 = [str8 substringFromIndex:9];NSLog(@"%@", str10);//"你問我愛你有多深"NSString *str11 = [str8 substringToIndex:8];NSLog(@"%@", str11);NSString *str12 = @"天再高又怎樣";NSString *str13 = @"踮起腳尖就更靠近陽光";//"天再高又怎樣踮起腳尖就更靠近陽光"NSString *str14 = [str12 stringByAppendingString:str13];NSLog(@"%@", str14);//"天再高又怎樣, 踮起腳尖就更靠近陽光"NSString *str15 = [str12 stringByAppendingFormat:@", %@", str13];NSLog(@"%@", str15);NSString *str16 = [NSString stringWithFormat:@"%@, %@", str12, str13];NSLog(@"%@", str16);字符串的替換
NSString *str17 = @"微笑擁抱每一天, 做向日葵溫暖的女子";NSString *str18 = [str17 stringByReplacingOccurrencesOfString:@"女子" withString:@"女漢子"];NSLog(@"%@", str18);NSString *str19 = @"baoshuang";NSString *str20 = @"wenbin";NSComparisonResult result = [str19 compare:str20];switch (result) {case NSOrderedAscending://右邊大NSLog(@"%@ < %@", str19, str20);break;case NSOrderedSame://相等NSLog(@"%@ = %@", str19, str20);break;case NSOrderedDescending://左邊大NSLog(@"%@ > %@", str19, str20);break;default:break;}比較字符串是否相等
NSString *str21 = @"NBA";NSString *str22 = @"CBA";BOOL isSame = [str21 isEqualToString:str22];if (isSame) {NSLog(@"相等");} else {NSLog(@"不相等");}判斷字符串是否以xxx開頭
NSString *str23 = @"http://www.2048.com";BOOL flag = [str23 hasPrefix:@"http://"];if (flag) {NSLog(@"OK");}判斷字符串是否以xxx結尾
NSString *str24 = @"guangen.png"; // NSString *str25 = @"";//判斷是否以png結尾, 如果是, 把png改為aviflag = [str24 hasSuffix:@".png"];if (flag) {NSString *str25 = [str24 stringByReplacingOccurrencesOfString:@"png" withString:@"avi"];NSLog(@"%@", str25);}NSString *str26 = @"I loVe you Not who yoU are, but for who I am before you";字符串改成小寫
NSString *str27 = [str26 lowercaseString];NSLog(@"%@", str27);改成大寫
NSString *str28 = [str26 uppercaseString];NSLog(@"%@", str28);首字母大寫
NSString *str29 = [str26 capitalizedString];NSLog(@"%@", str29);字符串類型轉換為基本類型(int, long, float, double)
NSString *str30 = @"123.456";int number1 = [str30 intValue];NSLog(@"%d", number1);NSInteger number2 = [str30 integerValue];NSLog(@"%ld", number2);float number3 = [str30 floatValue];NSLog(@"%f", number3);double number4 = [str30 doubleValue];NSLog(@"%lf", number4);轉換回去
NSString *str31 = [NSString stringWithFormat:@"%d", number1];NSLog(@"%@", str31);? ? ? ??NSMutableString, 可變字符串類(可以被編輯), 繼承于NSString
?創建可變字符串
NSMutableString *string = [[NSMutableString alloc]initWithCapacity:0];NSLog(@"%@", string);賦值
[string setString:@"人生沒有彩排, 每天都是現場直播"];NSLog(@"%@", string);字符串拼接 "---<輝哥語錄>"
[string appendString:@"---<輝哥語錄>"];NSLog(@"%@", string);字符串的插入
NSMutableString *string1 = [[NSMutableString alloc]initWithFormat:@"我與有個約會"];[string1 insertString:@"天使" atIndex:2];NSLog(@"%@", string1);字符串的刪除
NSMutableString *string2 = [[NSMutableString alloc] initWithFormat:@"我想找個女漢子"];[string2 deleteCharactersInRange: NSMakeRange(4, 1)];NSLog(@"%@", string2);字符串的替換
NSMutableString *string3 = [[NSMutableString alloc] initWithFormat:@"我想找個月薪1w的工作"];[string3 replaceCharactersInRange:NSMakeRange(4, 1) withString:@"日"];NSLog(@"%@", string3);?
?
?
?
?
?
轉載于:https://www.cnblogs.com/OrangesChen/p/4856313.html
總結
以上是生活随笔為你收集整理的查看文档(API) (NSString)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kosaraju算法、Tarjan算法分
- 下一篇: 基金单位净值小于1说明什么 这样的基金能