Encountered in the search box to filter illegal characters, the traditional use while loop to operate is too cumbersome, iOS nsstring there are related methods to solve this problem. as follows: Nscharacterset
*Donotwant
=[Nscharacterset charactersetwithcharactersinstring:@ "[email protected]#$%^&*+?/="]; TempString
=[[TempString componentsseparatedbycharactersinset:donotwant]componentsjoinedbystring: @ "]; Note: This method cannot be filtered by stringbytrimmingcharactersinset, it can only filter illegal characters at both ends of the string, but cannot filter out illegal characters contained in the string. Of course, in addition to filtering operations and determine whether there are illegal characters in the string operation, you can do as follows:
Nscharacterset *errorcharstr = [Nscharacterset
charactersetwithcharactersinstring:@ "[Email protected]#$%^&*+?/="];
Nsrange range = (nsrange) {65535,0};
range = [str rangeofcharacterfromset:errorcharstr];
if (Range.length > 0) {
return YES;
}
If the length property of range is greater than 0, the string contains illegal characters.
iOS Filters illegal strings