NSString Remove all newline and space

Source: Internet
Author: User
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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.