objective-c字串類NSString的使用

來源:互聯網
上載者:User

開發過程中,我們會遇到很多關於字串的操作,它是一個常用的資料類型,下面對它的用法進行了匯總:

    //1、建立常量字串。
    NSString *astring = @"This is a String!";

    //2、建立Null 字元串,給予賦值。

    NSString *astring = [[NSString alloc] init];
    astring = @"This is a String!";
    NSLog(@"astring:%@",astring);
  [astring release];

//3、在以上方法中,提升速度:initWithString方法

    NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
    NSLog(@"astring:%@",astring);
    [astring release];

 //4、用標準c建立字串:initWithCString方法

    char *Cstring = "This is a String!";
    NSString *astring = [[NSString alloc] initWithCString:Cstring];
    NSLog(@"astring:%@",astring);
    [astring release];

    //5、建立格式化字串:預留位置(由一個%加一個字元組成)

    int i = 1;
    int j = 2;
    NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];
    NSLog(@"astring:%@",astring);
    [astring release];

    //6、建立臨時字串

    NSString *astring;
    astring = [NSString stringWithCString:"This is a temporary string"];
    NSLog(@"astring:%@",astring);

    NSString *path = @"astring.text";
    NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
    NSLog(@"astring:%@",astring);
    [astring release];

    NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
    NSLog(@"astring:%@",astring);
    NSString *path = @"astring.text";    
    [astring writeToFile: path atomically: YES];
    [astring release];    

    //用C比較:strcmp函數

    char string1[] = "string!";
    char string2[] = "string!";
    if(strcmp(string1, string2) = = 0)
    {
        NSLog(@"1");
    }

   //isEqualToString方法    
    NSString *astring01 = @"This is a String!";
    NSString *astring02 = @"This is a String!";
    BOOL result = [astring01 isEqualToString:astring02];
    NSLog(@"result:%d",result);

   //compare方法(comparer返回的三種值)    
    NSString *astring01 = @"This is a String!";
    NSString *astring02 = @"This is a String!";    
    BOOL result = [astring01 compare:astring02] = = NSOrderedSame;    
    NSLog(@"result:%d",result);    
    //NSOrderedSame 判斷兩者內容是否相同

    NSString *astring01 = @"This is a String!";
    NSString *astring02 = @"this is a String!";
    BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;    
    NSLog(@"result:%d",result);
    //NSOrderedAscending 判斷兩對象值的大小(按字母順序進行比較,astring02大於astring01為真)

    NSString *astring01 = @"this is a String!";
    NSString *astring02 = @"This is a String!";
    BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;    
    NSLog(@"result:%d",result);     
    //NSOrderedDescending 判斷兩對象值的大小(按字母順序進行比較,astring02小於astring01為真)

//不考慮大 小寫比較字串1
    NSString *astring01 = @"this is a String!";
    NSString *astring02 = @"This is a String!";
    BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;    
    NSLog(@"result:%d",result);     
    //NSOrderedDescending判斷兩對象值的大小(按字母順序進行比較,astring02小於astring01為真)

//如何判斷字串為空白

NSString *urlString = [urlInput stringValue];

 
if (!urlString) { 
 
 
NSLog( @”NO INPUT.” );

 
} else { 
 
if ([urlString length] == 0 ) {

 
NSLog( @”NO INPUT.” );

 
} else {

 
}

 

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.