美文网首页
字符串的操作NSCharacterSet

字符串的操作NSCharacterSet

作者: 三岁就很乖 | 来源:发表于2016-05-09 10:03 被阅读172次
    
    //有字符串“      O~I^C_U>A      ",拆分出单个字母:
        NSString *str =@"      O~I^C_U>A       ";
        str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去掉前边和后边的空格
        NSCharacterSet * charSet = [NSCharacterSet characterSetWithCharactersInString:@"^~_>"];
        NSArray *arr = [str componentsSeparatedByCharactersInSet:charSet];
        //输出看效果
        [arr enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop) {
            NSLog(@"O~I^C_U>A->[%@]", obj);
        }];
        
        
        
        //关于invertedSet
        NSString *string =@"   ff3,45_69 . %><a     ";
        NSString * kNumber =@"0123456789.";
        NSCharacterSet *numberSet = [[NSCharacterSet characterSetWithCharactersInString:kNumber]invertedSet];
        //invertedSet方法是去反字符,把所有的除了kNumber里的字符都找出来(包含去空格功能)
        NSString *fieldString = [[string componentsSeparatedByCharactersInSet:numberSet]componentsJoinedByString:@""];
        NSLog(@"number__[%@]", fieldString);
    
    

    输出结果:

    O~I^C_U>A->[O]
    O~I^C_U>A->[I]
    O~I^C_U>A->[C]
    O~I^C_U>A->[U]
    O~I^C_U>A->[A]
    number__[34569.]
    
    

    相关文章

      网友评论

          本文标题:字符串的操作NSCharacterSet

          本文链接:https://www.haomeiwen.com/subject/eeqerttx.html