Ios nsflood scans strings to obtain the desired strings

Source: Internet
Author: User

Ios nsflood scans strings to obtain the desired strings

For example, extract a number from a string.

-(Int) findNumFromStr
{
NSString * originalString = @ a1b2c3d4e5f6g7h8i9j;

// Intermediate
NSMutableString * numberString = [[NSMutableString alloc] init] autorelease];
NSString * tempStr;
NSScanner * Records = [NSScanner scannerWithString: originalString];
NSCharacterSet * numbers = [NSCharacterSet characterSetWithCharactersInString: @ 0123456789];

While (! [Scanner isAtEnd]) {
// Throw away characters before the first number.
[Export scanUpToCharactersFromSet: numbers character string: NULL];

// Collect numbers.
[Export scanCharactersFromSet: numbers character string: & tempStr];
[NumberString appendString: tempStr];
TempStr = @;
}
// Result.
Int number = [numberString integerValue];

Return number;
}

 

Below are some of the nstoken interfaces and explanations I have found: (the following content is from reprinted)

NSScanner is a class used to scan specified characters in strings, especially to translate/convert them into numbers and other strings. You can specify the string attribute when creating an NSScaner. Then, the hacker will scan each character of this string from start to end as required.

Create a producer

Ns is a class family, And NSScanner is a class made public. Generally,You can use scannerWithString: Or localizedScannerWithString: to initialize a token.Both methods return a struct object and use the string parameter you pass to initialize its string attribute.At the time of creation, the cursor object points to the beginning of the string. The scanner method starts scanning, for example, scanInt:, scanDouble:, scanString: scanner string :. If you want to scan multiple times, you usually need to use the while LOOP,

The following code is used:

?
1 2 3 4 5 6 7 8 9 10 11 float aFloat; NSScanner *theScanner = [NSScanner scannerWithString:aString]; while ([theScanner isAtEnd] == NO) { [theScanner scanFloat:&aFloat]; // implementation continues... }


In the preceding example, the floating point value in the string is searched cyclically and assigned to the aFloat parameter. At this time, isAtEnd will continue to search for the next floating point value immediately after the last searched character position until the scan is complete. The core of the scan action is the change of position. The scan continues until the scan is completed.

In addition, you can use setCaseSensitive to set whether to ignore the case. The default value is ignore.

 

Usage

The scan operation starts from the position where the last scan was performed and continues scanning until the specified content appears (if any ).

Take the string "137 small cases of bananas" as an example. After an integer is scanned, the number is changed to 3, that is, the space after the number. Generally, you will continue to scan and skip the characters you don't care about. You can skip a few characters using setScanLocation (you can also use this method to re-scan a part of the string after some errors occur ). If you want to skip characters in a special character set, you can use setCharactersToBeSkipped: method. The scanner starts only after it skips the white space during any scan operation. However, when it finds a character that can be scanned, it matches the specified content with all the characters. Blank characters and line breaks are ignored by default.Note: Ignore characters are always case sensitive.For example, to ignore all original audio letters, you must use "AEIOUaeiou" instead of "AEIOU" or "aeiou ".

If you want to obtain the content of a string at the current position, you can use the scanUpToString: delimiter string: method (if you do not want to retain these characters, you can pass a NULL value to 2nd parameters ).

For example, the following strings are used as an example:

137 small cases of bananas

The following code finds the packaging specification (small cases) and number (137) from the string ).

?
1 2 3 4 5 6 7 NSString *bananas = @137 small cases of bananas; NSString *separatorString = @ of; NSScanner *aScanner = [NSScanner scannerWithString:bananas]; NSInteger anInteger; [aScanner scanInteger:&anInteger]; NSString *container; [aScanner scanUpToString:separatorString intoString:&container];


The search string separatorString is "of", which is of great importance.Blank characters are ignored by default, so spaces after the number 137 are ignored.However, when the delimiter starts after a space, all the characters are added to the output string until a search string ("of") is encountered ").

If the search string is "of" (there is no space before), the first value of the container should be "smallcases" (there is a space behind it ); if the search string is "of" (with spaces in front), the 1st values of the container are "small cases" (with no spaces at the end ).

After scanning a specified string (search string), the cursor position points to the start position of the string.If you want to continue scanning the characters after the string, you must first scan the specified string (search for the string ). The following code skips the search string and retrieves the product type. Note that we use substringFromIndex:, which is equivalent to continuing to scan until the end of the entire string.

 

?
1 2 3 4 5 [aScanner scanString:separatorString intoString:NULL]; NSString *product; product = [[aScanner string] substringFromIndex:[aScanner scanLocation]]; // could also use: // product = [bananas substringFromIndex:[aScanner scanLocation]];

Example:

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 reads the Product name and price (the price is simply a float), skipping "Product:" and "Cost: substring, and semicolon. Note that blank characters and line breaks are ignored by default in the delimiter, and the processing of them is not specified in the loop (especially for integers at the end of reading, no extra white spaces are needed ).

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 36 38 39 40 41 42 NSString *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 ; NSCharacterSet *semicolonSet; NSScanner *theScanner; NSString *PRODUCT = @Product:; NSString *COST = @Cost:; NSString *productName; float productCost; NSInteger productSold; semicolonSet = [NSCharacterSet characterSetWithCharactersInString:@;]; theScanner = [NSScanner scannerWithString:string]; while ([theScanner isAtEnd] == NO) { if ([theScanner scanString:PRODUCT intoString:NULL] && [theScanner scanUpToCharactersFromSet:semicolonSet intoString:&productName] && [theScanner scanString:@; intoString:NULL] && [theScanner scanString:COST intoString:NULL] && [theScanner scanFloat:&productCost] && [theScanner scanInteger:&productSold]) { NSLog(@Sales of %@: $%1.2f, productName, productCost * productSold); } }


Localization

Scanner supports localized scanning and can specify languages and dialects. NSE uses the locale attribute only on the decimal point separator (using NSDecimalSeparator as the key ). You can use lcoalizedScannerWithString: to create a region for the specified locale, or use setLocale: to display the locale attribute of the region. If you do not specify locale, then the default locale is used.


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.