ios字串的判斷
1.範圍的作用,看看一下結構體: [plain] view plaincopy typedef struct _NSRange { unsigned int location; unsigned int length; }NSRange;//其中location表示欄位存放該範圍的起始位置,length表示是該範圍內所含元素的個數。 例如:字串”Objective-C is a cool language“中,單詞”cool“可以用loaction為17,length為4的範圍來表示。也許由於Locatio欄位未被初始化,所以它 的值可以是NSNotFound,用來表示無意義範圍。 可以用3種方式建立新的NSRange:[plain] view plaincopy (1)直接給欄位賦值 NSRange range: range.location = 17; range.length = 4; (2)應用C的彙總結構賦值機制 NSRange range = {17,4}; (3)使用Cocoa提供的一個快捷函數NSMakeRange(); NSRange range = NSMakeRange(17,4); 2.幾何資料類型:[plain] view plaincopy (1) NSPoint和NSSize,首先看看它們的結構體: typedef struct _NSPoint{ float x ; float y; }NSPoint; typedef struct _NSSize{ float width; float height; }NSSIze; (2)矩形類型NSRect typedef struct _NSRect{ NSPoint origin; NSSize size; }NSRect; (3)它們也一樣有:建立資料類型的快捷函數,NSMakePoint(),NSMakeSize()和NSMakeRect(). 3.字串類型操作:
(1)建立字串,除了直接賦值建立NSString字串外,還可以通過:stringWithFormat方法來通過
格式字串和參數建立NSString的 +(id) stringWithFormat : (NSString *)format, .........例子如下: NSString *height; height = [NSString stringWithFormat : @"Your height is %d feet,%d inches",5,11]; 得到的字串是“Your height is 5 feet, 11inches”. 這裡需要注意的地方有兩個,第一就是這個方法後面的省略符號,表示這個方法可以接收多個以逗 號隔開的其他參數,第二就是方法聲明的開頭是一個+號,它代表建立該類的類對象,我們把這 種方法叫做“Factory 方法”。 (2)獲得字串大小:length,使用方法如下 unsigned int length = [height length]; 此方法可以準確無誤地處理國際字串。如俄文,中文,日文等。 (3)NSString字串比較操作: 1.-(BOOL) isEqualToString: (NSString *)aString; 使用如下 NSString *thing1 = @“hello 2”; NSString *thing2 ; thing2 = [NSString stringWithFormat : @"hello %d",5 ]; if ([thing1 isEqualToString : thing2]){ NSLog(@"They are the same!"); }//注意此方法是比較字串指標向的值,而“==”比較的是指標值。 2.-(NSComparisonResult)compare : (NSString *)string; 用法如下,首先先看看NSComparisonResult結構體: typedef enum _NSComparisonResult{ NSOrderedAscending = -1; NSOrderedSame, NSOrderedDEscending }NSComparisonResult; 例子:[@"zoinks" compare : @"jinies"]將會返回NSOrderedDescending (表示左側的字元在右側字元的後面) 3.-(NSComparisonResult) compare : (NSString *)string options : (unsigned)mask; options參數是一個位元遮罩,你可以使用位或運算子來添加選項標記。例子如下: if( [ thing1 compare : thing2 options : NSCaseInsensitiveSearch | NSNumericSearch ] == NSOrderedSame) { NSLog (@"They match!") ; } (4)字串內是否包含別的字串 1 .-(BOOL)hasPrefix:(NSString *)aString;//檢查字串是否以另一個字串開頭 -(BOOL)hasSuffix:(NSString *)aString;//檢查字串是否以另一個字串結尾 -(NSRange)rangeOfString: (NSString *)aString ;//將rangeOfString:發送給一個 NSString對象時,傳遞的參數是要尋找的字串,它會返回一個NSRange struct 來告訴你 與這個字串相匹配的 部分在哪裡以及能夠匹配上的字串個數。 例子:NSRange range; NSString *filename = @"draft-chapter.pages"; range = [fileName rangeOfString : @"chapter"]; 返回的range.start為6,range.length為7.如果傳遞的參數在接收字串中沒有找到,那 range.start則等於NSNotFound。 5.可變性,NSString是不可變的,這並不意味著你不能操作它們。Cocoa提供了一個NSString的子類,叫做NSMutableString(可以改變的字串) (1)-(id)stringWithCapacity: (unsigned)capacity; //注意這個容量只是給NSMutableString的一個建議。 例子:string = [NSMutableString stringWithCapcity : 42]; (2)有了一個可變的字串,那就可以對它執行各種操作了: 1.-(void)appendString : (NSString *)aString; 2.-(void)appendFormat : (NSString *)format , ......; 例子:NSMutableString *string; string = [NSMutableString stringWithCapacity: 50]; [ string appendString : @"Hello there"]; [ string appendFormat : @"human %d!",39 ]; 這段代碼的運行結果是string被賦值為“Hello there human 39!” 3.-(void)deleteCharactersInRange : (NSRange) range; 我們經常會把deleteCharactersInRange:和rangeOfString:連在一起使用,例子: NSMuableString *friends; friends = [ NSMutableString stringWithCapacity: 50]; [ friends appendString : @"JAmes BethLynn Jack Evan" ]; 接下來,找到JAck的字元範圍: NSRange jackRange; jackRange = [friends rangeOfString: @"Jack"]; jackRange.length++;//這句的意思是,把後面的空格也算上。 [ friends deleteCharactersInRange : jackRange ]; 結果為:“James BethLynn Evan”
typedef struct _NSRange { unsigned int location; unsigned int length; }NSRang
//其中location表示欄位存放該範圍的起始位置,length表示是該範圍內所含元素的個數