1. Introduction to Functions
The NSString class of Apple provides the Stringbytrimmingcharactersinset method to filter the special symbols at both ends of the string, and the function is declared as follows:
-(NSString *) Stringbytrimmingcharactersinset: (Nscharacterset *) set;
The official Apple Note for this function is: Returns a new string made by removing from both ends of the receiver characters contained in a given Cha Racter set. By official means we know that the function filters out the characters that match the members of the set set at both ends of the string .
2, the function uses the parameter of the function is the Nscharacterset type, so you need to define a Nscharacterset variable before using the function, of course, you can also use the system-provided Nscharacterset constants, such as Whitespaceandnewlinecharacterset, Whitespacecharacterset, etc.
3, examples are as follows:
A. Customize a nscharacterset that contains special symbols that need to be removed
Nscharacterset *set = [Nscharacterset charactersetwithcharactersinstring:@ "... "];
NSString *trimmedstring = [string Stringbytrimmingcharactersinset:set];
Trimmedstring is the filtered string
B. Using a collection of systems
NSString *text1 = [Textfield.text stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]];//spaces In addition to both ends
NSString *text = [Textfield.text stringbytrimmingcharactersinset:[nscharacterset Whitespaceandnewlinecharacterset] ;//Remove two paragraphs of space and enter the comment:
notes:
1, 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).
2, Whitespacecharacterset
Returns a character set containing only the in-line whitespace characters space (u+0020) and tab (U+0009).