OC ---- 字串 數組 iOS學習-----細碎知識點總結

來源:互聯網
上載者:User

標籤:

NSString *urlString = [NSString stringWithFormat:@"http://www.apple.com"];
    
    // 擷取字串長度
    NSLog(@"長度:%lu", [urlString length]);

// 求子字串
    NSLog(@"%@", [urlString substringFromIndex:4]);
    NSLog(@"urlString = %@",urlString);
// 拼接字串
    NSLog(@"======%@", [urlString stringByAppendingString:@"1234"]);

// 替換字串
    NSLog(@"--===-===%@", [urlString stringByReplacingOccurrencesOfString:@"www" withString:@"m"]);

// 字串相等
    NSLog(@"-----%d", [urlString isEqualToString:@"http://www.apple.com"]);

// 字串比較
    NSLog(@"=====%ld", [urlString compare:@"h"]);

 

#pragma mark - NSMutableString:可變字串
// 拼接字串
    [mutableString appendString:@"2234"]; // 在原有字串的基礎上進行操作
    NSLog(@"---%@", mutableString);
    
    // 插入
    [mutableString insertString:@"abcd" atIndex:0];
    NSLog(@"--==-%@", mutableString);
    
    // 刪除
    [mutableString deleteCharactersInRange:range];
    NSLog(@"--=+====-%@", mutableString);
    
    // 替換
    [mutableString replaceCharactersInRange:range withString:@"www"];
    NSLog(@"--=====-%@", mutableString);
    
    // 對於NSString來說,操作的方法一般是以stringBy開頭,方法有傳回值,得到一個新的字串,對於NSMutableString來說,一般操作的是自身

 

數組  擷取數組中元素的個數, nill後面的元素不算在內

   // 擷取數組中對應位置的元素
  //  NSLog(@"====%@", [arry objectAtIndex:10]);
數組越界
   
   NSLog(@"---%@", [arry lastObject]);
   NSLog(@"====%@", [arry firstObject]);

#pragma mark - NSMutableArry
// 執行個體方法建立對象
    NSMutableArray *arry1 = [[NSMutableArray alloc] initWithObjects:@"西遊記", @"紅樓夢", @"三國演義", @"水滸傳", nil];
    // 使用便利器建立對象
    NSMutableArray *arry2 = [NSMutableArray arrayWithObjects:@"趙雲", @"馬超", nil];
    NSMutableArray *arry3 = [NSMutableArray arrayWithArray:arry];
    
    // 添加元素
    [arry2 addObject:@"關羽"];
    NSLog(@"%@", arry2);
    
    // 插入
    [arry2 insertObject:@"劉備" atIndex:0];
    
    
    // 刪除
    [arry2 removeObjectAtIndex:3];
    
    // 替換
    [arry2 replaceObjectAtIndex:0 withObject:@"孫尚香"];
    
    // 交換兩個元素的位置
    [arry2 exchangeObjectAtIndex:0 withObjectAtIndex:2];
    
    // 遍曆數組裡面的元素
    for (int i = 0; i < [arry2 count]; i++) {
        NSLog(@"---%@", [arry2 objectAtIndex:i]);
    }
    // 快速枚舉
    for (NSString *string in arry2) { // 在使用for 。。。in進行快速枚舉的過程中不允許改變集合裡面元素的個數。
        NSLog(@"%@", string);
    }

// 字串截取
    NSArray *arry = [@"2015:08:10 17:45:00"componentsSeparatedByString:@":"];
    NSLog(@"%@", arry);


#pragma mark - NSNumber: 值對象
NSNumber *intNumber = [NSNumber numberWithInt:20];
    NSArray *arry = [NSArray arrayWithObjects:intNumber, nil];
    NSLog(@"%@", intNumber);
    NSLog(@"%d", [intNumber intValue]);  // 把值對象轉換為對象
    
    
    NSNumber *number = [NSNumber numberWithInt:20];
    NSLog(@"%p, %p", intNumber, number); // 享元機制,潛在記憶體泄露
    
    
    NSRange rang = NSMakeRange(3, 9);
    NSValue *value1 = [NSValue valueWithRange:rang];
    NSLog(@"value = %@", value1);
    
    NSLog(@"loc = %lu", [value1 rangeValue].location);
    
    char *str = "一二三四";
    NSValue *value2 = [NSValue valueWithPointer:str];
    NSLog(@"value2 = %@", value2);
    NSLog(@"value2 = %p", [value2 pointerValue]);
    NSLog(@"----%@", NSStringFromClass([Book class])); // 把類名轉換為字串
    NSLog(@"====%@", NSStringFromRange(rang));  // 把結構體轉換為字串

OC ---- 字串 數組 iOS學習-----細碎知識點總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.