Nscharacterset and its variable version nsmutablecharacterset, which represent a set of Unicode characters in an object-oriented manner, are often combined with nsstring and nsscanner to filter on different characters, Delete or split operations, the following is the class method provided by Nscharacterset
Creating a standard Character Set+Alphanumericcharacterset+Capitalizedlettercharacterset+Controlcharacterset+Decimaldigitcharacterset+Decomposablecharacterset+Illegalcharacterset+Lettercharacterset+Lowercaselettercharacterset+Newlinecharacterset+Nonbasecharacterset+Punctuationcharacterset+Symbolcharacterset+Uppercaselettercharacterset+Whitespaceandnewlinecharacterset+whitespacecharactersetCreating a Character Set For URL Encoding +Urlfragmentallowedcharacterset+Urlhostallowedcharacterset+Urlpasswordallowedcharacterset+Urlpathallowedcharacterset+Urlqueryallowedcharacterset+urluserallowedcharactersetCreating a Custom Character Set +charactersetwithcharactersinstring:+Charactersetwithrange:–invertedsetCreating and managing Character sets As Bitmap representations +charactersetwithbitmaprepresentation:+charactersetwithcontentsoffile:–bitmaprepresentationtesting Set membership–characterismember:– Hasmemberinplane:–issupersetofset:–longcharacterismember:
Remove spaces
NSString -stringByTrimmingCharactersInSet:
is a method you need to keep in mind. It often passes in NSCharacterSet +whitespaceCharacterSet
or +whitespaceAndNewlineCharacterSet
out to remove the white space symbol of the input string's kinsoku.
1 // remove spaces at both ends of a string 2 + (NSString *) Trimstringwhitespace: (NSString *) str{3 Nscharacterset *whitespace = [Nscharacterset Whitespacecharacterset]; 4 str = [str stringbytrimmingcharactersinset:whitespace]; 5 return str; 6 }
1 //determines whether a string contains only numbers2+ (BOOL) Isturecustomnum: (NSString *) Customnum3 {4Nscharacterset *namecharacters = [[Nscharacterset charactersetwithcharactersinstring:@"0123456789"] Invertedset];5Nsrange Usernamerange =[Customnum rangeofcharacterfromset:namecharacters];6 if(Usernamerange.location! =nsnotfound) {7 //contains special characters8 returnNO;9}Else{Ten returnYES; One } A - } - the - - //determines whether a string contains only letters, numbers, and underscores -+ (BOOL) isvalidatestring: (NSString *) str{ +Nscharacterset *namecharacters = [[Nscharacterset charactersetwithcharactersinstring:@"_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"] Invertedset]; -Nsrange Usernamerange =[str rangeofcharacterfromset:namecharacters]; + if(Usernamerange.location! =nsnotfound) { A //contains special characters at returnNO; -}Else{ - returnYES; - } -}
This method only removes the opening and closing whitespace symbols, and if you want to remove the extra space between the words, look at one of the following methods
1NSString *string=@"Lorem ipsum dolar sit amet.";2 string= [stringStringbytrimmingcharactersinset:[nscharacterset Whitespacecharacterset]];3 4Nsarray *components = [stringComponentsseparatedbycharactersinset:[nscharacterset Whitespacecharacterset]];5components = [Filteredarrayusingpredicate:[nspredicate Predicatewithformat:@"Self <> '"]];6 7 string= [Components componentsjoinedbystring:@" "];
First, remove the whitespace from the string, then NSString -componentsSeparatedByCharactersInSet:
divide the string into one using a space, then NSArray
use one to remove the empty string, NSPredicate
and finally, use a NSArray -componentsJoinedByString:
single whitespace to re-spell the array into a string. Note: This method only applies to English as a space-delimited language.
don't use NSCharacterSet
participle. use CFStringTokenizer
it instead.
Language does not always use spaces as a dividing word. Although the language used in the space demarcation is actually very extensive.
...... Even in a space-delimited language, participle has some ambiguous boundary conditions, especially compound words and punctuation marks.
This is just a description: If you want to divide a string into meaningful words, then use CFStringTokenizer
(or enumerateSubstringsInRange:options:usingBlock:
).
Parsing data from a string
NSScanner
is a class used to parse data for arbitrary or semi-structured strings. When you create a scanner for a string, you can specify which characters to omit, which prevents those characters from being included in the parsed results in a variety of ways.
For example, you want to parse out the opening time from such a string:
mon-thurs:8:00-18:00 fri:7:00-17:00 sat-sun:10:00-15:00
You will enumerateLinesUsingBlock:
use one NSScanner
to parse it like this:
1Nsmutablecharacterset *skippedcharacters =[Nsmutablecharacterset Punctuationcharacterset];2 [Skippedcharacters formunionwithcharacterset:[nscharacterset whitespacecharacterset];3 4[Hours enumeratelinesusingblock:^ (NSString *line, BOOL *stop) {5Nsscanner *scanner =[Nsscanner scannerwithstring:line];6 [Scanner setcharacterstobeskipped:skippedcharacters];7 8NSString *startday, *Endday;9 Nsuinteger Starthour, Startminute, Endhour, Endminute;Ten One[Scanner Scancharactersfromset:[nscharacterset Lettercharacterset] intostring:&StartDay]; A[Scanner Scancharactersfromset:[nscharacterset Lettercharacterset] intostring:&Endday]; - -[Scanner scaninteger:&Starthour]; the[Scanner scaninteger:&Startminute]; -[Scanner scaninteger:&Endhour]; -[Scanner scaninteger:&Endminute]; -}];
We first construct one from the set of whitespace character sets and the punctuation character set NSMutableCharacterSet
. Tells you NSScanner
to ignore these characters to greatly reduce the necessary logic to parse these characters.
scanCharactersFromSet:
The incoming alphabetic character set gets the number of days to start and end (optional) within one week of each item. scanInteger
Similarly, the next consecutive integer value is obtained.
NSCharacterSet
and NSScanner
allows you to code quickly and confidently. The two are perfect combinations.
Nscharacter? Set is used in string manipulation