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