iOS之NSCanner的基本用法--掃描字串
NSScanner是一個類,用於在字串中掃描指定的字元,尤其是把它們翻譯/轉換為數字和別的字串。可以在建立NSScaner時指定它的string屬性,然後scanner會按照你的要求從頭到尾地掃描這個字串的每個字元。
剛建立時scanner物件指向字串的開頭。scanner方法開始掃描,比如scanInt:,scanDouble:,scanString:intoString:。如果你要想掃描多遍,通常需要使用while迴圈,
float aFloat;
NSScanner *theScanner = [NSScanner scannerWithString:aString];//建立一個scanner物件;
while ([theScanner isAtEnd] ==NO) {//判斷是否掃描到結尾,如果沒有掃描到結尾,迴圈掃描
[theScanner scanFloat:&aFloat];//搜尋字串中的浮點值,並賦值給aFloat引數。掃描到某個型別就結束,(如果還要掃描其他的浮點數,接著上次繼續執行一次程式碼);
迴圈掃描-----這個時候isAtEnd便會緊接上一次搜尋到的字元位置繼續搜尋看是否存在下一個浮點值,直至掃描結束。掃描動作的核心就是位置的變動。位置不停地在掃描中移動,直至結束掃描。
}
=================
NSString
NSString *separatorString = @"of";
NSScanner *aScanner = [NSScannerscannerWithString:bananas];
NSInteger anInteger;
[aScanner scanInteger:&anInteger];//掃描integer型別的字元,存到anInteger中
NSString *container;
[aScanner scanUpToString:separatorString
====================
NSString *string = @"Product: Acme Potato Peeler; Cost: 0.98 73\n\
Product: Chef Pierre Pasta Fork; Cost: 0.75 19\n\
Product: Chef Pierre Colander; Cost: 1.27 2\n";
NSCharacterSet *semicolonSet;
NSScanner *theScanner;
NSString *PRODUCT = @"Product:";
NSString *COST = @"Cost:";
NSString *productName;
float productCost;
NSInteger productSold;
semicolonSet = [NSCharacterSetcharacterSetWithCharactersInString:@";"];//字符集的取值範圍是分號(也就是字串中的每個字元)
NSLog(@"----%@",semicolonSet);
theScanner = [NSScannerscannerWithString:string];
while ([theScanner isAtEnd] == NO)//判斷是否到了自符串結尾
{
//掃描到了PROUDUCT:,分號,COST:過濾掉(不儲存)
if ([theScanner scanString:PRODUCT intoString:NULL]//掃描PRODUCT
&&
[theScanner scanUpToCharactersFromSet:semicolonSet
intoString:&productName]//接著上面PRODUCT後,掃描semicolonSet(即分號)前面的字元
&&
[theScanner scanString:@";"intoString:NULL]//掃描分號
&&
[theScanner scanString:COST intoString:NULL]//掃描COST
&&
[theScanner scanFloat:&productCost]//掃描float型別
&&
[theScanner scanInteger:&productSold])//掃描integer型別
{
NSLog(@"Sales of--- %@: $%1.2f", productName, productCost * productSold);
}
}
====================//NSCanner下面的屬性和方法
@property (readonly, copy) NSString *string;//scanner型別轉換成字串型別
@property NSUInteger scanLocation;//設定開始掃描的位置
@property (nullable, copy) NSCharacterSet *charactersToBeSkipped;//掃描的時候需要忽略的值
@propertyBOOL caseSensitive;//是否區分大小寫
@property (nullable, retain)id locale;
- (instancetype)initWithString:(NSString *)string;
//NSCanner分類中的屬性和方法:
- (BOOL)scanInt:(nullableint *)result;//掃描int型別,並且賦值給result型別
- (BOOL)scanInteger:(nullableNSInteger *)result;//掃描integer型別
- (BOOL)scanLongLong:(nullablelonglong *)result;
- (BOOL)scanUnsignedLongLong:(nullableunsignedlonglong *)result ;
- (BOOL)scanFloat:(nullablefloat *)result;
- (BOOL)scanDouble:(nullabledouble *)result;
- (BOOL)scanHexInt:(nullableunsigned *)result; // Optionally prefixed with "0x" or "0X"
- (BOOL)scanHexLongLong:(nullableunsignedlonglong *)result; // Optionally prefixed with "0x" or "0X"
- (BOOL)scanHexFloat:(nullablefloat *)result; // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix.
- (BOOL)scanHexDouble:(nullabledouble *)result; // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix.
- (BOOL)scanString:(NSString *)string intoString:(NSString *_Nullable * _Nullable)result;//從scan中掃描出string放入result中,如果不需要儲存string,則result傳NULL
- (BOOL)scanCharactersFromSet:(NSCharacterSet *)set intoString:(NSString * _Nullable * _Nullable)result;//從scan中掃描出string放入result中,如果不需要儲存string,則result傳NULL
- (BOOL)scanUpToString:(NSString *)string intoString:(NSString *_Nullable * _Nullable)result;//從scan中掃描出string之外的資料放入result中,如果不需要儲存string,則result傳NULL
- (BOOL)scanUpToCharactersFromSet:(NSCharacterSet *)set intoString:(NSString * _Nullable * _Nullable)result;//從scan中掃描出set之外的資料放入result中,如果不需要儲存string,則result傳NULL
@property (getter=isAtEnd,readonly) BOOL atEnd;//是否掃描到了結尾;
+ (instancetype)scannerWithString:(NSString *)string;//初始化一個scanner
+ (id)localizedScannerWithString:(NSString *)string;//初始化一個scanner