首页 > 代码库 > 字符串过滤

字符串过滤

   1. //有字符串“A~B^C_D>E",拆分出单个字母:

            NSString *str =@"      A~B^C_D>E       ";

            str = [str stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];//去掉前边和后边的空格

            NSCharacterSet * charSet = [NSCharacterSetcharacterSetWithCharactersInString:@"^~_>"];

            NSArray *arr = [strcomponentsSeparatedByCharactersInSet:charSet];

           //输出看效果

            [arr enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop) {

                NSLog(@"A~B^C_D->[%@]", obj);

            }];

         2.//关于invertedSet

            NSString *string =@"     123,45_6  7/8>9a     ";

            NSString * kNumber =@"0123456789.";

//NSCharacterSet *numberSet = [NSCharacterSet characterSetWithCharactersInString:kNumber];

            NSCharacterSet *numberSet = [[NSCharacterSetcharacterSetWithCharactersInString:kNumber]invertedSet];

            //invertedSet方法是去反字符,把所有的除了kNumber里的字符都找出来(包含去空格功能)

            NSString *fieldString = [[stringcomponentsSeparatedByCharactersInSet:numberSet]componentsJoinedByString:@""];

            NSLog(@"number__[%@]", fieldString);

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81 } span.s1 { color: #6122ae } span.s2 { color: #000000 } span.s3 { }</style>

字符串过滤