iOS Learning Note (33) basic use of--nsstring

Source: Internet
Author: User

A string is cut into an array componentsseparatedbystring

Two iOS string removal of special symbols stringbytrimmingcharactersinset should be used for account login etc.

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

Line 1th is to remove the 2-terminal space

Line 2nd is to remove the carriage return

Search method in three NSString rangofstring

  1. NSString *str1 = @"Can you \ \ Speak 中文版";
  2. NSString *str = @"\ n";
  3. //Search in str1 this string \ n to determine if there is
  4. if ([str1 rangeofstring:str].location! = nsnotfound) {
  5. NSLog (@"This string has \ n");
  6. }
  7. //rangeofstring The preceding argument is the string to be searched, followed by the character to be searched
  8. //nsnotfound indicates that a request operation is something or item is not found, or does not exist

iOS Learning Note (33) basic use of--nsstring

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.