iOS NSString只保留字符串中的数字

NSString * str = @"123/21年123";
    
    NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    
    //获取字符串中的数字
    
    str = [[str componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
    NSLog(@"结果是:%@",str);

鸣谢:https://www.cnblogs.com/lulushen/p/9884873.html

posted on 2019-08-10 15:04  高彰  阅读(1476)  评论(0编辑  收藏  举报

导航