I. Description
During the development of iOS, some of us need to remove all the blanks in the string and enter the carriage return, and most bloggers give the following approach:
-(NSString *) Removespaceandnewline: (NSString *) str
{
nsstring *temp = [Str stringbytrimmingcharactersinset:[ Nscharacterset Whitespacecharacterset]];
NSString *text = [temp stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]];
return text;
This method can only get rid of the strings at both ends of the space and carriage return, for the middle of the string there is no way. If you want to remove all the blanks and carriage returns in the string, we can simply use the substitution method to replace all the blanks and carriage returns with null. Use the relevant APIs to:
-(NSString *) stringbyreplacingoccurrencesofstring: (NSString *) target withstring: (NSString *) Replacement
Second, the implementation of the code note that this code will be all the blanks and strings removed, if only to remove both ends can use the above code
-(NSString *) Removespaceandnewline: (NSString *) str
{
nsstring *temp = [str stringbyreplacingoccurrencesofstring:@ "" withstring:@ ""];
temp = [Temp stringbyreplacingoccurrencesofstring:@ "\ r" withstring:@ "];
temp = [Temp stringbyreplacingoccurrencesofstring:@ "\ n" withstring:@ "];
return temp;
}