我有一个包含信用卡号的 UITextfield。我想将用户在 UITextField 中输入的信用卡号转换为破折号。一个例子是:
发件人:1021 3151 1641 3546
至:1021-3151-1641-3546
我将把方法放在 textFieldShouldEndEditing 方法中。有人会为此提供正确的代码吗?
我有一个包含信用卡号的 UITextfield。我想将用户在 UITextField 中输入的信用卡号转换为破折号。一个例子是:
发件人:1021 3151 1641 3546
至:1021-3151-1641-3546
我将把方法放在 textFieldShouldEndEditing 方法中。有人会为此提供正确的代码吗?
尝试用“-”替换空格
yourStr=[yourStr stringByReplacingOccurrencesOfString:@" " withString:@"-"];
-(NSString*)appendDash:(NSString*)str
{
NSMutableString *string = [NSMutableString stringWithString:str];
//remove the white spaces of original string
[string replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [string length])];
int num = [string length];
for (int i = 4;i<= num; i++) {
[string insertString:@"-" atIndex:i];
i+=4;
}
return string;
}