A string is cut into an array componentsseparatedbystring
Two iOS string removal of special symbols stringbytrimmingcharactersinset should be used for account login etc.
First, the previous example:
NSString * str1 =[nameinput.text Stringbytrimmingcharactersinset:[nscharactersetwhitespaceandnewlinecharacterset]] ;
NSString * str2 =[passwdinput.text Stringbytrimmingcharactersinset:[nscharactersetwhitespaceandnewlinecharacterset ]];
[Self.delegate loginactioninview:self name:str1 PASSWD:STR2];
Reprinted from: http://blog.csdn.net/aiyongyyy/article/details/8269546
You can use the Stringbytrimmingcharactersinset function in iOS to filter special symbols in a string
First define a nscharacterset, including special symbols that need to be removed.
Nscharacterset *set = [nscharacterset charactersetwithcharactersinstring: @ "@/:; () ¥" ", []{}#%-*+=_\\|~<> $€^ ' @#$%^&* () _+ ' \ ""];
Because there are full-width and half-width symbols in the nsstring, some symbols include full-width and half-width
Then call Stringbytrimmingcharactersinset
NSString *trimmedstring = [string stringbytrimmingcharactersinset:set];
Trimmedstring is the filtered string.
----------------------------------------------------------
Http://blog.sina.com.cn/s/blog_5421851501014xif.html
RemoveusernameThe space in the table Newline,nextline
The code is as follows: (three lines of code)
Nscharacterset *whitespace = [NscharactersetWhitespaceandnewlinecharacterset];
NSString *username= [Musernamefield stringvalue];
Username = [usernameStringbytrimmingcharactersinset: whitespace];
Notes:
Stringbytrimmingcharactersinset:
Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
Whitespaceandnewlinecharacterset
Returns a character set containing only the whitespace characters space (u+0020) and tab (u+0009) and the newline and next Line characters (u+000a–u+000d, u+0085).
Alternatively, you can useWhitespacecharactersetReplaceWhitespaceandnewlinecharactersetDifference newline nextline
Whitespacecharacterset
Returns a character set containing only the in-line whitespace characters space (u+0020) and tab (U+0009).
NSString *temptext = [messageTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *text = [temptext stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];
Line 1th is to remove the 2-terminal space
Line 2nd is to remove the carriage return
Search method in three NSString rangofstring
- NSString *str1 = @"Can you \ \ Speak 中文版";
- NSString *str = @"\ n";
- //Search in str1 this string \ n to determine if there is
- if ([str1 rangeofstring:str].location! = nsnotfound) {
- NSLog (@"This string has \ n");
- }
- //rangeofstring The preceding argument is the string to be searched, followed by the character to be searched
- //nsnotfound indicates that a request operation is something or item is not found, or does not exist
iOS Learning Note (33) basic use of--nsstring