Some operations on NSString strings

Source: Internet
Author: User

When using strings, some strings we need to delete or replace some of the characters we don't need, then we use the Stringbyreplacingoccurrencesofstring method:

    @" @moxue " ;     *first = [str stringbyreplacingoccurrencesofstring:@"@ " withstring:@ "h "]; // Replace @ with H    NSString *second = [str stringbyreplacingoccurrencesofstring:@ "@" withstring:  @""]; // Remove String @

Another way: By customizing a nscharacterset that contains the characters to be removed:

@" moxue#moxue*moxue    moxue$$$";     *Set = [Nscharacterset charactersetwithcharactersinstring:@ "]; // set the characters    that need to be removed STR2 = [str2 stringbytrimmingcharactersinset:set]; // invoking methods for filtering    NSLog (@ "%@", str2);

In addition Nscharacterset has some of his own methods:

// Remove both spaces [str2 Stringbytrimmingcharactersinset:[nscharacterset Whitespacecharacterset]; // Remove Carriage return

String cutting is an array:

@" mo Xue, Wang Shu, burning day, Meteor "  *namearr = [name componentsseparatedbystring:@ ","];

Convert the above array to a string, with "," as the split point:

NSString *name2 = [Namearr componentsjoinedbystring:@ ","];

Some operations on NSString strings

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.