There are times when you need to process strings. Now let's summarize the process of deleting special characters from a string .
1.stringByTrimmingCharactersInSet This method can only handle the special characters in the two parts before and after the string. But there's nothing in the middle of the string .
NSString *str =@ "123:: daf\n-N";
NSString * str2 = [strstringbytrimmingcharactersinset:[nscharactersetwhitespaceandnewlinecharacterset]];// Whitespaceandnewlinecharacterset remove both ends of the space and carriage return, some other parameters can be self-examination
NSLog (@ "str2:%@", str2);
NSString *str1 =@ e:e \ GE::::::;
NSString *STR3 = [str1stringbytrimmingcharactersinset:[nscharactersetcharactersetwithcharactersinstring:@ ":"]];// Remove the colon at both ends
NSLog (@ "str3:%@", STR3);
2. Remove all special characters or characters that are not special in the string
The following code is to delete all the spaces in the string, carriage returns:
NSString *str = @ "123:: daf\n";
nsmutablestring *mutstr = [NSMUTABLESTRINGSTRINGWITHSTRING:STR];
Nsrange range = {0,str.length};
[Mutstr replaceoccurrencesofstring:@ "" withstring:@ "" options:NSLiteralSearchrange:range];
Nsrange range2 = {0,mutstr.length};
[Mutstr replaceoccurrencesofstring:@ "\ withstring:@" "options:NSLiteralSearchrange:range2];
NSLog (@ "%@", mutstr);