美文网首页
字符串每4个一个空格

字符串每4个一个空格

作者: 贺乾龙 | 来源:发表于2016-09-21 14:21 被阅读69次

    NSNumber *number = [NSNumber numberWithLongLong:1234567890098765];

    NSNumberFormatter *formatter = [NSNumberFormatter new];

    [formatter setUsesGroupingSeparator:YES];设置用组分隔

    [formatter setGroupingSize:4];//四个一组

    [formatter setGroupingSeparator:@" "];组间用空格隔开

    NSString *string = [formatter stringFromNumber:number];

    string : 1234 5678 9009 8765

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if (textField == bankCardNumTextField) {

    // 四位加一个空格

    if ([string isEqualToString:@""]) { // 删除字符

    if ((textField.text.length - 2) % 5 == 0) {

    textField.text = [textField.text substringToIndex:textField.text.length - 1];

    }

    return YES;

    } else {

    if (textField.text.length % 5 == 0) {

    textField.text = [NSString stringWithFormat:@"%@ ", textField.text];

    }

    }

    return YES;

    }

    return YES;

    }

    相关文章

      网友评论

          本文标题:字符串每4个一个空格

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