五.Foundation框架(2)--NSString/NSMutableString

#import <Foundation/Foundation.h>程序員

int main(int argc, const char * argv[]) {api

    

     @autoreleasepool {數組

          棧區 - 由系統進行管理,程序員沒法操控app

          堆區 - 由程序員進行管理性能

         NSString *str = @"abcdefg";     將會存放在棧區,不須要主動釋放內存.net

          如下建立的都須要人工進行釋放內存component

         NSString *strA = [[NSString alloc]init];orm

         strA = @"abcdefghijgk";內存

         NSString *strB = [[NSString alloc]initWithString:strA];ci

          c語言字符串轉換爲oc字符串

         char *c = "abv";

         NSString *strC = [[NSString alloc]initWithUTF8String:c];

         

          如下建立的都會自動釋放 原理是會在方法調用以後加上[xx autorelease]

         NSString *strD = [NSString stringWithFormat:@"abcd%d",3];

         NSString *strE = [NSString stringWithString:strD];

         NSString *strF = [NSString stringWithUTF8String:c];

         

          子串系列

          [strB substringFromIndex:1]; 從某個位置到結束的全部字符串

          [strB substringToIndex:1];  從開始到某個位置的全部字符串

          [strB substringWithRange:(NSRange){0,1}]; 截取某個範圍內的字符串

          [strB componentsSeparatedByString:@"/"];   以 '/' 爲標準進行字符串切割,而後切割出來的每一部分字符串放入到數組當中

          比較系列

         [strB isEqualToString:@"b"];    判斷兩個字符串是否相等

         NSComparisonResult cResult = [strB compare:strA]; 1 前者大 0二者相等 -1前者小

         if(cResult == NSOrderedAscending)

             NSLog(@"left<right");

         else if(cResult == NSOrderedDescending)

             NSLog(@"left>right");

         else if(cResult == NSOrderedSame)

             NSLog(@"left==right");

         

          文件讀取系列

         NSString *path = @"/Users/whunf/Desktop/IT12/OCWithProperty/OCWithProperty/office資料.c";

         NSString *strG = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

          首字母大寫,其他小寫

         str = @"HEllo woRld";

         NSLog(@"原來的:%@,轉換後的:%@",str,[str capitalizedString]);

          是否包含

         NSRange rng = [strB rangeOfString:@"fe"];

          判斷xx是否爲空

         if(rng.location == NSNotFound)

             NSLog(@"not found");

          將結構體包裝成類,便於輸出

         NSLog(@"%@",NSStringFromRange(rng));

          NSLog(@"%ld,%ld",rng.location,rng.length);

         str = @"1";

          類型轉換

         int intValue = [str intValue];

         NSInteger integerValue = [str integerValue];

         float floatValue = [str floatValue];

         double doubleValue = [str doubleValue];

         _Bool boolValue = [str boolValue];

         long long longValue = [str longLongValue];

         const char *cs = [str UTF8String];

          字符串長度

         NSInteger len = [str length];

          轉換成大寫

         str = @"b";

         NSLog(@"%@",[str uppercaseString]);

          轉換成小寫

         str = @"B";

         NSLog(@"%@",[str lowercaseString]);

          判斷是否以xx爲開頭

         BOOL ret = [path hasPrefix:@"/Users"];

         NSLog(@"%d",ret);

          判斷是否以xx爲結尾

         ret =[path hasSuffix:@".d"];

          NSLog(@"%d",ret);

          是否包含某個字符串

            [str containsString:@「d"];

    }

    return 0;

}

上面講解了,NSString的經常使用方法。

       下面是NSMutableString的經常使用方法:

         NSMutableString 可變

             單詞記憶

         追加 append  插入insert  刪除 delete remove

         方位/下標 index  座標 location  格式 Format  

 

        NSMutableString *mStr = [[NSMutableString alloc]init];

         分配字符串大小,提高性能,但不是不能夠超過這個範圍。

        NSMutableString *mString = [[NSMutableString alloc]initWithCapacity:10];

         mStr = @"def"; 可變字符串不能進行直接賦值

        NSLog(@"%@",mStr);

         字符串添加系列

         在某個位置插入字符串

        [mStr insertString:@"abc" atIndex:0];

        NSLog(@"%@",mStr);

         在原有的字符串後面進行追加內容

        [mStr appendString:@"def"];

        NSLog(@"%@",mStr);

         在原有的字符串後面格式化後追加內容

        [mStr appendFormat:@"deg%d",3];

        NSLog(@"%@",mStr);

         刪除某個範圍內的字符

        [mStr deleteCharactersInRange:(NSRange){0,3}];

        NSLog(@"%@",mStr);

 

實例講解:

 1.建立一個字符串 "OCWith/OCWithProperty/office.c",利用string當中的子串截取方法,將每一個/以前的單詞截取出來,放入到數組當中

 

int main(int argc, const char * argv[]) {

     @autoreleasepool {

NSString *str =@"OCWith/OCWithProperty/office.c";

         int loc = 0;                截取開始的座標

         int len = 0;                截取的長度

          建立一個數組,用於儲存截取出來的字符串

         NSArray *arr = [NSArray array];

         for (int i = 0; i<str.length; i++) {

              獲取當前位置的字符

             if([str characterAtIndex:i] == '/')

             {

                  獲取range範圍內的字符串

                 NSString *temp = [str substringWithRange:(NSRange){loc,len}];

                  將獲取的字符串組合到數組中

                 arr = [arr arrayByAddingObject:temp];

                 loc = i+1;          截取開始的位置變化

                 len = -1;           長度重設

             }

             len++;

         }

         NSLog(@"%@",arr);

          ----------------------------------

          經過自帶的方法實現字符串截取

         arr = [str componentsSeparatedByString:@"/"];

         NSLog(@"%@",arr);

         

         2.建立一個方法,用來獲取文件名稱和擴展名

          獲取文件名稱

         NSString *temp = [str substringWithRange:(NSRange){loc,len}];

         NSString *fileName = @"";       用於儲存文件名稱

         NSString *fileType = @"";       用於儲存文件類型

         loc = 0;

         len = 0;

         for (int i = 0; i<temp.length; i++) {

              獲取當前位置的字符

             if([temp characterAtIndex:i] == '.')

             {

                  獲取range範圍內的字符串

                 fileName = [temp substringWithRange:(NSRange){loc,len}];

                 loc = i+1;

                 len = -1;

             }

             len++;

         }

         fileType = [temp substringWithRange:(NSRange){loc,len}];

         NSLog(@"文件名稱:%@,文件類型:%@",fileName,fileType);

    }

    return 0;

}

相關文章
相關標籤/搜索