在iOS系統,NSString可能是最常用的對象,很多用法跟其他語言不一樣。
字符串對象NSString
使用格式創建字符串
+ (id)stringWithFormat:(NSString *)format... - (id)initWithFormat:(NSString *)format... NSString *str = "hello"; NSString *string = [NSString stringWithFormat:@"%@ world",str]; NSLog(string);
結果:
hello world
常用的替換符
%@ NSString實例
%d,%D,%i 整數
%4d,%4D,%4i 格式化整數
%ld,%lD,%li 長整數
%u,%U 無符號整數
%x 將無符號整數以十六進制小寫字母顯示
%X 將無符號整數以十六進制大寫字母顯示
%f 小數
%c 字符
%s C語言字符串
%% 顯示%字符本身
范圍集合NSRange
定義
typedef struct _NSRange { unsigned int location; unsigned int length; }NSRange;
NSMakeRange函數
這個函數比較特殊 返回一個NSRange的對象。
NSMakeRanger(unsigned int location,unsigned int length);
例如:
NSRange range = NSMakeRanger(0,5); NSLog(@"location is %d,length is %d",range.location,range.length);
查找
如果找到就返回范圍,否則NSRange的location項被設置為NSNotFound
- (NSRange)rangeOfString:(NSString *)subString; - (NSRange)rangeOfString:(NSString *)subString option:(unsigned)mask; - (NSRange)rangeOfString:(NSString *)subString option:(unsigned)mask range:(NSRange)range; //mask常用選項列表 //NSCaseInsensitiveSearch 不區分字母大小寫 //NSLiteralSearch 對字符串進行字節單位的比較,一般可提高檢索速度 //NSBackwardsSearch 從范圍的末尾開始檢索 //NSAnchoredSearch 僅檢索制定范圍的前部。忽略字符串中間的檢索字符 NSString *string = @"hello world"; NSRange range = [string rangeOfString:@"he"]; if(range.location != NSNotFound) { NSLog(@" location=%d,length=%d",range.location,range.length); }
截取字符串
NSString //返回字符串開頭至index位的字符串 不包含索引位 - (NSString *)substringToIndex:(unsigned)index; //返回字符串第index位至末尾的字符串 包含索引位 - (NSString *)substringFromIndex:(unsigned)index; //返回字符串中范圍range內的字符串 - (NSString *)substringWithRange:(NSRange)range; //包含索引位 NSString *string = [string substringWithRange:NSMakeRange(5,2)];
比較字符串
NSString *String1 = @"NSStringInformation.txt"; //hasPrefix 前綴比較 [String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO"); //hasSuffix 後綴比較 [String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO"); //isEqualToString 完全比較 if([string1 isEqualToString:@""]) { NSLog(@"string1 is blank"); }
替換字符串
NSString *newString = [oldString stringByReplacingOccurrencesOfString:@"x" withString:@"y"];
分離字符串成數組
NSString *string = @"A|B|C|D"; NSArray *array = [string componentsSeparatedByString:@"|"];
讀取文本文件
NSString + (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error //自動釋放內存 - (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error NSString *string = [NSString stringWithContentsOfFile:@"/user/test/yw.txt" encoding:NSUTF8StringEncoding error:&error]; if(string){}
輸出文本文件
NSString - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error //參數 atomically 暫時將文件保存到輔助文件中 //path The file to which to write the receiver. If path contains a tilde (~) character, you must expand it withstringByExpandingTildeInPath before invoking this method. //擴展路徑 NSString *Path = @"~/NSData.txt"; NSString *absolutePath = [Path stringByExpandingTildeInPath]; NSLog(@"absolutePath:%@",absolutePath); NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]); //文件擴展名 NSString *Path = @"~/NSData.txt"; NSLog(@"Extension:%@",[Path pathExtension]);