Brief introduction
Nsscanner is a class that is used to scan a string for specified characters, especially translating/converting them to numbers and other strings. You can specify the string property of Nsscaner when you create it, and then scanner will scan each character of the string from start to finish as you ask.
Nsscanner Official Documents
The Nsscanner class is an abstract parent class for a class family that provides a program interface for an object that scans values from a NSString object.
The Nsscanner object interprets and translates the characters of the NSString object into number and string type values. When you create a Nsscanner object, you assign it a character (string), and when you get the content from the Nsscanner object, it iterates through the string, from the beginning to the end.
Because of the properties of the class family, the scanner object is not an instance of the Nsscanner class, but an instance of its private subclass. Although the class of the scanner object is private, its interface is public (the abstract parent class has declared it). The original method of Nsscanner is all of the methods listed below in the string and configuring a Scanner methods.
When a Nsscanner object scans a string, you can omit certain characters by setting the property characterstobeskipped. Characters that are in the ignored character set will be skipped before the string is scanned. The default ignore character is a space and carriage return character.
The non-scanned string can be obtained by [[Scanner string] Substringfromindex:[scanner Scanlocation]].
Scanner Create
Nsscanner is a class family, and Nsscanner is a kind of public. In general, you can initialize a scanner with the scannerwithstring: or localizedscannerwithstring: Method.
+ (instancetype)scannerWithString:(NSString *)aString+ (id)localizedScannerWithString:(NSString *)aString- (instancetype)initWithString:(NSString *)aString
- Scannerwithstring, the return value is a Nsscanner object that has scanned the astring string, which is set by calling the Initwithstring setting to scan the string;
- Localizedscannerwithstring, the return value is the Nsscanner object that scans the string through the user's default locale, and the method is also set to scan strings by calling initwithstring;
- Initwithstring, the return value is a Nsscanner object that is initialized by scanning the astring
Attribute details
@property (readonly , copy) nsstring *string @property Nsuinteger scanlocation @property bool Casesensitive @property (copy) Nscharacterset *characterstobeskipped @property (retain) id locale
- Scanlocation, where the next scan starts, if the value is outside the range of string, will cause nsrangeexception, which is useful when rescanning after an error occurs.
- CaseSensitive, whether the case-sensitive flag is distinguished from the string. Default is no, note: This setting is not applied to the skipped character set.
- Characterstobeskipped, the character set that was skipped while scanning, the default is blank and enter. The skipped character set takes precedence over the scanned character set: For example, a scanner character set that is skipped is a space, and by Scanint: To find the integer number in a string, the first thing to do is not to scan, but to skip the space until the decimal data or other characters are found. The Skip function fails when the character is scanned. If you scan the characters and the skipped characters are the same, the results will be unknown. The skipped character is a unique value, scanner does not apply the Ignore case function to it, does not use these characters to do some combination, if you want to ignore all the vowel characters when scanning the character, it is necessary to do so (for example: Set the CharSet to "Aeiouaeiou"};
- The locale of Locale,scanner has an effect on how it distinguishes values from strings, and it distinguishes between integers and fractional parts of floating-point data through the locale's decimal separator. A scanner with no locale is used for the non-fixed domain value. New scanner If no locale is set, the default locale is used.
Detailed method
- (BOOL) Scancharactersfromset: (Nscharacterset *) Scanset intostring: (NSString* _nullable *) stringvalue;-(BOOL) Scanuptocharactersfromset: (Nscharacterset *) Stopset intostring: (NSString* _nullable *) stringvalue;-(BOOL) Scanstring: (NSString*) string intostring: (NSString* _nullable *) stringvalue;-(BOOL) Scanuptostring: (NSString*) stopstring intostring: (NSString* _nullable *) stringvalue;-(BOOL) Scandecimal: (Nsdecimal *) decimalvalue;-(BOOL) Scandouble: (Double*) doublevalue;-(BOOL) Scanfloat: (float*) floatvalue;-(BOOL) Scanhexdouble: (Double*) result;-(BOOL) Scanhexfloat: (float*) result;-(BOOL) Scanhexint: (unsigned int*) intvalue;-(BOOL) Scanhexlonglong: (unsigned Long Long*) result;-(BOOL) Scanint: (int*) intvalue;-(BOOL) Scaninteger: (Nsinteger*) value;-(BOOL) Scanunsignedlonglong: (unsigned Long Long*) Unsignedlonglongvalue;@property(Getter=isatend,ReadOnly)BOOLAtEnd;
- Scancharactersfromset:intostring: The characters matched in the scan string and the Nscharacterset character set are individually matched by character, for example, the Nscharacterset character set is @ "TEST123DMO", The scanner string is @ "123test12demotest", so all the characters in the string are in the character set, so the address that the pointer points to stores the contents "123test12demotest"
- Scanuptocharactersfromset:intostring: Scans the string until it encounters a character in the Nscharacterset character set, and the address that the pointer points to stores content that precedes the skipped character set character
- Scanstring:intostring: Start scanning from the current scan position, determine whether the scan string from the current position can scan to and pass in the same string of characters, if you can scan to return yes, the pointer to the address of the store is the contents of this string. For example, scanner string content is 123abc678, the incoming string content is ABC, if the current scan location is 0, then the scan is not, but if the scan location is set to 3, you can scan it.
- Scanuptostring:intostring: When scanning from the current scan location, scanning to the same string as the passed-in string, stops, and the pointer points to the address that stores the content before the incoming string is encountered. For example, scanner string content is 123abc678, the incoming string content is ABC, and the content stored is 123
- Scandecimal: Scan for values of type Nsdecimal, more information about Nsdecimal type values can be viewed: nsdecimalnumber
- Scandouble: Scan double-precision floating-point characters, overflow and non-overflow are considered valid floating-point data. In the case of overflow, scanner will skip all numbers, so the new scan location will be behind the entire floating-point data. The address that the double pointer points to stores data that is scanned, including the Huge_val or –huge_val at the time of overflow, which is 0.0 when no overflow occurs.
- Scanfloat: Scan single-precision floating-point characters with scandouble
- Scanhexdouble: Scan double-precision hexadecimal type, overflow and non-overflow are considered valid floating-point data. In the case of overflow, scanner will skip all numbers, so the new scan location will be behind the entire floating-point data. The address that the double pointer points to stores data that is scanned, including the Huge_val or –huge_val at the time of overflow, which is 0.0 when no overflow occurs. When the data is received in the format%a or%a, double hexadecimal characters must be preceded by 0x or 0X.
- Scanhexint scans the hexadecimal unsigned integer, and the unsigned int pointer points to an address value of the value scanned, containing the uint_max when the overflow occurred.
- Scanhexlonglong with Scanhexdouble
- Scanint Scan integer, overflow is also considered to be a valid integer, the value of the address pointed to by the int pointer is the value scanned, containing the Int_max or int_min when it overflows.
- Scaninteger with Scanint
- Scanlonglong scan Longlong type, overflow is also considered to be a valid integer type, the value of the address that the Longlong pointer points to is the value scanned, containing the Llong_max or llong_min when overflow.
Scanner use
Nsscanner This class, which is used to scan a string for a specified character. You can specify its string property when you create Nsscanner, and then scanner scans each character in the string as required from start to finish. The scan action causes the scanner to move through the string from beginning to end until the entire string is scanned or the specified content is scanned.
When the scan is stopped, the scanner's position is not set to where the string begins. The next scan will start at the location where the last scan stopped. Scanlocation to specify where the scan will begin, characterstobeskipped is to specify the characters that do not need to be scanned.
NSString*bananas = @"123.321abc137d efg/hij KL";NSString*separatorstring = @"FG";BOOLResult Nsscanner *ascanner = [Nsscanner Scannerwithstring:bananas];//Scan string //Stop when scanning to the specified string, return the result to the string before the specified string NSLog(@"Scanner location:%lu", Ascanner. Scanlocation);NSString*container; result = [Ascanner scanuptostring:separatorstring intostring:&container];NSLog(@"Scan succeeded:%@", [email protected]"YES":@"NO");NSLog(@"Results returned by scan:%@", container);NSLog(@"Scanner location:%lu", Ascanner. Scanlocation);//Scan integer //will continue scanning where the last scan ended NSLog(@"-------------------------------------1");NSLog(@"Scanner location:%lu", Ascanner. Scanlocation);NsintegerAninteger; result = [Ascanner scaninteger:&aninteger];NSLog(@"Scan succeeded:%@", [email protected]"YES":@"NO");NSLog(@"Scan returned results:%ld", Aninteger);NSLog(@"Scanner location:%lu", Ascanner. Scanlocation);//Scan integer //Position the scanner in the first place The scanner will start scanning by default at the end of the previous scan, rather than starting from the first position //When scanning to a character that is not an integer, the scan will stop (if the location starting the scan is not an integer, the scan will be stopped directly) NSLog(@"-------------------------------------2"); Ascanner. Scanlocation=0;//Position the scanner in the first place NSLog(@"Scanner location:%lu", Ascanner. Scanlocation);NsintegerAnInteger2; result = [Ascanner scaninteger:&aninteger2];NSLog(@"Scan succeeded:%@", [email protected]"YES":@"NO");NSLog(@"Scan returned results:%ld", AnInteger2);NSLog(@"Scanner location:%lu", Ascanner. Scanlocation);//Scan floating point number //When scanning to a character that is not an integer, the scan will stop (if the location starting the scan is not an integer, the scan will be stopped directly) NSLog(@"-------------------------------------3"); Ascanner. Scanlocation=0;//Position the scanner in the first place NSLog(@"Scanner location:%lu", Ascanner. Scanlocation);floatAfloat; result = [Ascanner scanfloat:&afloat];NSLog(@"Scan succeeded:%@", [email protected]"YES":@"NO");NSLog(@"Results returned by scan:%f", afloat);NSLog(@"Scanner location:%lu", Ascanner. Scanlocation);NSLog(@"-------------------------------------4");NSLog(@"scanned string:%@", Ascanner. String);NSLog(@"Scanner location:%lu", Ascanner. Scanlocation);NSLog(@"Scan to end:%@", Ascanner. Isatend[Email protected]"YES":@"NO");NSLog(@"-------------------------------------5"); Ascanner. Scanlocation=0;//Position the scanner in the first place NSLog(@"Scanner location:%lu", Ascanner. Scanlocation);NSString*STR; Nscharacterset *characterset = [Nscharacterset charactersetwithcharactersinstring:@"/"]; result = [Ascanner scanuptocharactersfromset:characterset intostring:&str];NSLog(@"Scan succeeded:%@", [email protected]"YES":@"NO");NSLog(@"Results returned by scan:%@", str);NSLog(@"Scanner location:%lu", Ascanner. Scanlocation);
Printing results:
Example 1: Scanning numbers
NSString * numStr = @"a 1 b 2 c 3 d 4 e 5 f 6 o"; NSScanner * scanner = [NSScanner scannerWithString:numStr]; NSCharacterSet * numSet = [NSCharacterSet decimalDigitCharacterSet]; whileNO == [scanner isAtEnd]) { if ([scanner scanUpToCharactersFromSet:numSet intoString:NULL]) { int num; if ([scanner scanInt:&num]) { NSLog(@"num=%d, %ld",num, scanner.scanLocation); } }
Printing results:
Example 2: Filtering numbers
NSString* Numstr = @"a 1 B 2 c 3 D 4 E 5 F 6 o"; Nsscanner * scanner = [Nsscanner scannerwithstring:numstr];//By default, the scanner skips all whitespace and line breaks. But we want to skip the numbers here.Nscharacterset * Numset = [Nscharacterset decimaldigitcharacterset]; Scanner. characterstobeskipped= Numset; while(NO= = [Scanner Isatend]) {if([Scanner Scanuptocharactersfromset:numset intostring:NULL]) {intNumif([Scanner Scanint:&num]) {NSLog(@"num=%d,%ld", NUM, scanner. Scanlocation); }Else{NSLog(@"No results"); } } }}
Printing results:
Example 3
Suppose you have the following string:
Product:acme Potato Peeler; cost:0.98 73? Product:chef Pierre Pasta Fork; cost:0.75 19? Product:chef Pierre Colander; cost:1.27 2?
The following code demonstrates the operation of reading the product name and price (the price is simply read as a float), skipping the "Product:" and "Cost:" substrings, as well as semicolons. Note that because scanner ignores whitespace and newline characters by default, no processing is specified in the loop (especially for the integer at the end of the read, which does not require processing additional whitespace characters).
NSString*string = @"Product:acme Potato Peeler; cost:0.98 73\nproduct:chef Pierre Pasta Fork; cost:0.75 19\nproduct:chef Pierre Colander; cost:1.27 2\n \ n "; Nscharacterset *semicolonset; Nsscanner *thescanner;NSString*product = @"Product:";NSString*cost = @"Cost:";NSString*productname;floatProductcost;NsintegerProductsold; Semicolonset = [Nscharacterset charactersetwithcharactersinstring:@";"]; Thescanner = [Nsscanner scannerwithstring:string]; while([thescanner isatend] = =NO) {if([Thescanner scanstring:product intostring:NULL] &&[thescanner scanuptocharactersfromset:semicolonset intostring:&productname] && [TheScan NER scanstring:@";"Intostring:NULL] && [Thescanner scanstring:cost intostring:NULL] && [Thescanner scanfloat:&productcost] && [Thescanner scaninteger:&product Sold]) {NSLog(@"Sales of%@: $%1.2f", ProductName, Productcost * productsold); } }
Printing results:
Reference article:
Ios:nsscanner, a strange condition to judge the weapon!
Basic usage of the Nsscanner class
iOS development-usage of Nsscanner
The nsscanner of the condition judgment of the class