OC中字元竄操作

秋風掃dota發表於2013-12-11
/--------操作字串--NSString(靜態字串)---------------------
    NSString *Beijing= @"北京歡迎您";        //字串的宣告

    NSString *log=@"北京歡迎您a"; 

    [NSString stringWithFormat:@"I am '%@'",Beijing];     //字串格式化,可以把int,float等型別轉化為字元竄

    NSString *zhui = [Beijing stringByAppendingString:@"哈哈哈"];        //字串追加
    bool b=[Beijing isEqualToString:log];                               //字串比較
    NSString *hh = @"http://www.sina.com.cn";
    if([hh hasPrefix:@"http"]){                                          //查詢以http開頭的字串
        NSLog(@"含有http");
    }else{
        NSLog(@"沒有http");
    }
    NSString *ss = @"123";
    int a = [ss intValue]+13;                                   //字串轉int型
    double dd = [ss doubleValue]+33.3;                          //字串轉double型
    NSLog(@"%g",dd);
//字串轉陣列 
    NSString *zifuchuan =@"one,two,three,four";
    NSLog(@"string:%@",zifuchuan);
    NSArray *array = [zifuchuan componentsSeparatedByString:@","];
//    NSLog(@"array:%@",array);                             //輸出整個陣列中所有元素 
    NSString *value = [array objectAtIndex:0];          //取出第0個元素
    NSLog(@"value:%@",value);
//陣列轉字串
    NSString * zifuchuan2 = [array componentsJoinedByString:@","];
 

相關文章