IOS NSString Remove other characters (space carriage return)

Source: Internet
Author: User


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);


Related Article

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.