您好,登錄后才能下訂單哦!
//初始化 //iniWithString------Returns an NSString object initialized by copying the characters from another given string. // 返回一個NSString對象初始化復制來自另一個給定字符串的字符。 NSString *str = @"liuyafang"; NSString *str1 = [[NSString alloc] initWithString:str]; NSLog(@"str1 = %@", str1); //計算字符串長度 //length------Returns the number of Unicode characters in the receiver. //返回接收器中Unicode字符的數量 NSInteger count = [str length]; //可見長度 NSLog( @"%ld", count); //initWithFormat: //Returns an NSString object initialized by using a given format string as a template into which the remaining argument values are substituted. //返回一個NSString對象初始化使用給定的格式字符串作為模板,其余參數值代替。 NSString *str2 = @"dapingmu"; NSString *str3 = [[NSString alloc] initWithFormat:@"liuyafang"]; NSLog(@"str2 = %@", str2); NSLog(@"str3 = %@", str3); //判斷是否以指定字符開頭或者結尾 BOOL pre = [str3 hasPrefix:@"liu"]; NSLog(@"%d", pre); BOOL suf = [str3 hasSuffix:@"fang"]; NSLog(@"%d", suf); //截取字符串的長度和起始位置----Finds and returns the range of the first occurrence of a given string within the receiver. // NSRange rag =[str3 rangeOfString:@"liu"]; NSLog(@"length = %ld, lacation = %ld", rag.length, rag.location); //截取從輸入的起始位置開始輸出 NSString *sub = [str3 substringFromIndex:3]; NSLog(@"%@", sub); //截取到輸入的位置并輸出 NSString *subb = [str3 substringToIndex:3]; NSLog(@"%@", subb); //截取一個范圍字符串 NSRange aa = {0 , 4}; NSString *ran1 = [str3 substringWithRange:aa]; NSString *ran = [str3 substringWithRange:NSMakeRange(0, 5)]; //NSMakeRange(0, 5) 范圍的起始位置和末尾位置 NSLog(@"%@", ran1); NSLog(@"%@", ran); //拼接字符串 NSString *str4 = @" very good !"; NSString *app = [str stringByAppendingString:str4]; NSLog(@"%@", app); NSLog(@"%@",str); NSString *b = [str stringByAppendingFormat:@"%@==%d", str,5]; //格式化拼接,,,有問題 ,, NSLog(@"%@",b); //替換字符串 NSString *rep = [str3 stringByReplacingOccurrencesOfString:@"ya" withString:@"xiao"]; NSLog(@"%@", rep); //轉換成小寫的 NSString *lowe= [@"ljlkmJNnhjnHhhbhHnbjjbnghUKJkj" lowercaseString]; NSLog(@"%@", lowe); //首字母轉換成大寫; NSString *cap = [@"ad Hj da ajda ajdl la " capitalizedString]; NSLog(@"%@",cap); NSMutableString *mutabal = [NSMutableString stringWithCapacity:5]; NSLog(@"%@", mutabal); NSMutableString *mutabal1 =[NSMutableString stringWithFormat:@"liuyafang"]; NSLog(@"%@", mutabal1); //可變字符串拼接 //Adds a constructed string to the receiver.---添加一個構造字符串到接收方。 [mutabal1 appendFormat:@"good"]; NSLog(@"%@", mutabal1); [mutabal1 appendFormat:@"%@", @"good"]; NSLog(@"%@", mutabal1); //Adds to the end of the receiver the characters of a given string.--增加了接收機的最后一個給定字符串的字符。 [mutabal1 appendString:@"what!"]; NSLog(@"%@", mutabal1); [mutabal1 stringByAppendingString:@"nimei"]; NSLog(@"----------%@", mutabal1); //刪除范圍字符串 // Removes from the receiver the characters in a given range.----刪除來自接收者的角色在一個給定的范圍內。 NSRange aaa = {0, 3}; [mutabal1 deleteCharactersInRange:aaa]; NSLog(@"%@", mutabal1); //NSMakeRange-------Creates a new NSRange from the specified values.--創建一個新的NSRange指定值。 [mutabal1 deleteCharactersInRange:NSMakeRange(0, 3)]; NSLog(@"%@",mutabal1); //Replaces the characters of the receiver with those in a given string.----替換字符的接收器與給定的字符串。 [mutabal1 setString:@"liu"]; NSLog(@"%@", mutabal1); //作業1。。判斷是否以EfGk結尾,如果是替換成WXYZ,然后轉變成小寫 NSString *exercise = @"aBcD_EfGk"; BOOL a1 = [exercise hasSuffix:@"EfGk"]; NSLog(@"%d", a1); NSString *exer = [exercise stringByReplacingOccurrencesOfString:@"EfGk" withString:@"WXYZ"]; NSLog(@"---====>%@", exer); NSString *exer1 = [exer lowercaseString]; NSLog(@"------>%@",exer1); //作業1。2判斷是都以png結尾,如果是替換成jpg,如果不是則添加.jpg NSMutableString *page = [NSMutableString stringWithFormat:@"xiaoliu.png"]; BOOL aa11 = [page hasSuffix:@"png"]; NSLog(@"%d",aa11); if ( aa11 == 1) { NSString *page1 = [page stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"]; NSLog(@"%@", page1); }else{ [page appendString:@".jpg"]; NSLog(@"%@", page); } //數組初始化,,獲取元素個數 NSArray *arr = [NSArray arrayWithObjects:@"ss",@"dd",@"aa", nil]; NSLog(@"%@", arr); NSInteger con = [arr count]; NSLog(@"%ld", con); // 根據對象獲得所引致 //Returns the lowest index whose corresponding array value is equal to a given object.--- NSInteger dd = [arr indexOfObject:@"dd"]; NSLog(@"%ld", dd); //根據所引致獲得對象 //Returns the object located at the specified index. NSArray *ppp = [arr objectAtIndex:0]; NSLog(@"0000000%@", ppp); ///-------------可變數組--------------/// NSMutableArray *mutarr = [NSMutableArray arrayWithCapacity:5]; NSLog(@"%@", mutarr); NSMutableArray *mt = [NSMutableArray arrayWithObjects:@"aaa", @"bbb", @"ccc", @"ddd", nil]; NSMutableArray *mm = [NSMutableArray arrayWithObjects:@"ee", @"qq", nil]; NSLog(@"%@", mt); //添加元素 //Inserts a given object at the end of the array. [mt addObject:@"fff"]; NSLog(@"%@", mt); //插入元素 // Inserts a given object into the array's contents at a given index. [mt insertObject:@"ooo" atIndex:2]; NSLog(@"%@", mt); //數組連接,,, //Adds the objects contained in another given array to the end of the receiving array’s content. [mt addObjectsFromArray:mm]; NSLog(@"%@", mt); //刪除元素 [mt removeObjectAtIndex:2]; NSLog(@"%@", mt); //替換元素 [mt replaceObjectAtIndex:0 withObject:@"ttttt"]; NSLog(@"%@", mt); //交換兩個指定位置對元素 [mt exchangeObjectAtIndex:0 withObjectAtIndex:1]; NSLog(@"%@", mt); //圖書管理 // NSMutableArray *library = [NSMutableArray arrayWithObjects:@"tushu1",@"tushu2", @"tushu3", @"tushu4", nil]; // NSInteger count1 = [library count]; // NSInteger ddd; // NSLog(@"1-添加圖書2-刪除圖書3-修改圖書4-查找圖書5-查看圖書"); // scanf("%ld", &ddd); // if (ddd == 1) { // [library addObject:@"tushu5"]; // NSLog(@"%@", library); // } // if (ddd == 2) { // [library removeObject:@"tushu3"]; // NSLog(@"%@", library); // } // if (ddd == 3) { // [library setObject:@"tushu0" atIndexedSubscript:0]; // NSLog(@"%@", library); // } // if (ddd == 4) { // NSInteger place = [library indexOfObject:@"tushu3"]; // NSLog(@"%ld", place); // } // if (ddd == 5) { // for (int i = 0; i < count1; i++) { // NSLog(@"%@",library[i]); // } // } //正式作業1; NSString *jiequ = @"20|http://www.baidu.com"; NSString *neww = [jiequ substringFromIndex:3]; //從這個位置開始截取, 截取前面輸出后面。 NSLog(@"%@", neww); NSString *new = [jiequ substringToIndex:2]; //截取到這個位置 NSLog(@"%@", new); //將文件改寫成213 NSString *qing = @"文藝青年"; NSString *a213 = [qing stringByReplacingOccurrencesOfString:@"文藝" withString:@"213"]; NSLog(@"%@", a213);
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。