Remove special symbols in IOS string stringbytrimmingcharactersinset

Source: Internet
Author: User

First, the previous example:

NSString * str1 =[nameinput.text Stringbytrimmingcharactersinset:[nscharactersetwhitespaceandnewlinecharacterset]] ;

NSString * str2 =[passwdinput.text Stringbytrimmingcharactersinset:[nscharactersetwhitespaceandnewlinecharacterset ]];

[Self.delegate loginactioninview:self name:str1 PASSWD:STR2];

Reprinted from: http://blog.csdn.net/aiyongyyy/article/details/8269546

You can use the Stringbytrimmingcharactersinset function in iOS to filter special symbols in a string

First define a nscharacterset, including special symbols that need to be removed.

Nscharacterset *set = [nscharacterset charactersetwithcharactersinstring: @ "@/:; () ¥" ", []{}#%-*+=_\\|~<> $€^? ' @#$%^&* () _+ ' \ "";

Because there are full-width and half-width symbols in the nsstring, some symbols include full-width and half-width

Then call Stringbytrimmingcharactersinset

NSString *trimmedstring = [string stringbytrimmingcharactersinset:set];

Trimmedstring is the filtered string.

----------------------------------------------------------

Http://blog.sina.com.cn/s/blog_5421851501014xif.html

RemoveusernameThe space in the table Newline,nextline
The code is as follows: (three lines of code)

Nscharacterset *whitespace = [NscharactersetWhitespaceandnewlinecharacterset];

NSString *username= [Musernamefield stringvalue];

Username = [usernameStringbytrimmingcharactersinset: whitespace];



Notes

Stringbytrimmingcharactersinset:
Returns a new string made by removing from both ends of the receiver characters contained in a given character set.

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

Alternatively, you can useWhitespacecharactersetReplaceWhitespaceandnewlinecharactersetDifference newline nextline
Whitespacecharacterset
Returns a character set containing only the in-line whitespace characters space (u+0020) and tab (U+0009).

NSString *temptext = [messageTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *text = [temptext stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];

Use the Stringbytrimmingrightcharactersinset method to remove the rightmost space of a string.

C code
    1. NSLog (@ "%@", [@ "ABC 123" Stringbytrimmingrightcharactersinset:[nscharacterset Whitespacecharacterset]]);

How do I get rid of a string @ "0.012300" at the far right of "0"? This is the typical float format into a string after the form, it is best to remove the rightmost "0".

Here's how to fix it:

C code
    1. [@ "0.012300" Stringbytrimmingrightcharactersinset:[nscharacterset charactersetwithcharactersinstring:@ "0"];

Remove special symbols in IOS string stringbytrimmingcharactersinset

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.