对过滤方法的整理

在我们编写程序的时候经常会把数据库中的数据加一过流以供我们的使用,包括电话号码的过滤等等

1.一种比较简单的过滤方法

 NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"[]{}( #%-*+=_)\\|~(<>$%^&*)_+ "];
     string = [[string componentsSeparatedByCharactersInSet: doNotWant]componentsJoinedByString: @""];

2.第二种方法

 NSMutableString * str = [NSMutableString stringWithString:string];
    NSRange rang = [str rangeOfString:@" "];
    while (rang.location != NSNotFound)
    {
        [str deleteCharactersInRange:rang];
        rang = [str rangeOfString:@" "];
    }
    rang = [str rangeOfString:@"-"];
    while (rang.location != NSNotFound)
    {
        [str deleteCharactersInRange:rang];
        rang = [str rangeOfString:@"-"];
    }
    rang = [str rangeOfString:@"("];
    while (rang.location != NSNotFound)
    {
        [str deleteCharactersInRange:rang];
        rang = [str rangeOfString:@"("];
    }
    rang = [str rangeOfString:@")"];
    while (rang.location != NSNotFound)
    {
        [str deleteCharactersInRange:rang];
        rang = [str rangeOfString:@")"];
    }

3.第三种方法

string = [string stringByReplacingOccurrencesOfString:@")" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];

以上就是比较常用的过滤的方法   在过滤特殊符号中我们需要注意的是@“(” 我们在去掉这个符号的同时会留下一个空格这个空格事则是一个点;

你可能感兴趣的:(ios,数据库)