Remove special symbols at both ends of a string by Stringbytrimmingcharactersinset

Source: Internet
Author: User
Tags character set set set

1. Introduction to Functions

The NSString class of Apple provides the Stringbytrimmingcharactersinset method to filter the special symbols at both ends of the string, and the function is declared as follows:

-(NSString *) Stringbytrimmingcharactersinset: (Nscharacterset *) set;

The official Apple Note for this function is: Returns a new string made by removing from both ends of the receiver characters contained in a given Cha  Racter set. By official means we know that the function filters out the characters that match the members of the set set at both ends of the string .


2, the function uses the parameter of the function is the Nscharacterset type, so you need to define a Nscharacterset variable before using the function, of course, you can also use the system-provided Nscharacterset constants, such as Whitespaceandnewlinecharacterset, Whitespacecharacterset, etc.


3, examples are as follows:

A. Customize a nscharacterset that contains special symbols that need to be removed

Nscharacterset *set = [Nscharacterset charactersetwithcharactersinstring:@ "... "];

NSString *trimmedstring = [string Stringbytrimmingcharactersinset:set];

Trimmedstring is the filtered string

B. Using a collection of systems

NSString *text1 = [Textfield.text stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]];//spaces In addition to both ends

NSString *text = [Textfield.text stringbytrimmingcharactersinset:[nscharacterset Whitespaceandnewlinecharacterset] ;//Remove two paragraphs of space and enter the comment:


notes:

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

2, Whitespacecharacterset
Returns a character set containing only the in-line whitespace characters space (u+0020) and tab (U+0009).






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.