1. 程式人生 > >iOS之NSCanner的基本用法--掃描字串

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

*bananas = @"137 0.5  small cases of bananas 嗷嗷版本";

NSString *separatorString = @"of";

NSScanner *aScanner = [NSScannerscannerWithString:bananas];

NSInteger anInteger;

    [aScanner scanInteger:&anInteger];//掃描integer型別的字元,存到anInteger

NSString *container;

    [aScanner scanUpToString:separatorString

intoString:&container];//接著上面的integer型別掃描(如果前面沒有integer掃描,則從頭掃描),預設忽略integer後面的空格,如果separatorString中的字串of前面沒有空格(或有一個空格)/of後面沒有空格(或有一個空格)則掃描integerseparatorString之間的所有字元;如果separatorString中的字串of前面有兩個或以上空格/of後面有兩個或以上空格,則掃描到integer後面的所有字元;


====================

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