我有一个像@" 1234123412341234"这样的字符串,我需要在每4个字符之间追加空格。
@"1234 1234 1234 1234"
即我需要NSString
类似Visa卡类型。我试过这样但我没有得到我的结果。
-(void)resetCardNumberAsVisa:(NSString*)aNumber
{
NSMutableString *s = [aNumber mutableCopy];
for(int p=0; p<[s length]; p++)
{
if(p%4==0)
{
[s insertString:@" " atIndex:p];
}
}
NSLog(@"%@",s);
}
答案 0 :(得分:7)
以下是NSString
@interface NSString (NRStringFormatting)
- (NSString *)stringByFormattingAsCreditCardNumber;
@end
@implementation NSString (NRStringFormatting)
- (NSString *)stringByFormattingAsCreditCardNumber
{
NSMutableString *result = [NSMutableString string];
__block NSInteger count = -1;
[self enumerateSubstringsInRange:(NSRange){0, [self length]}
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if ([substring rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]].location != NSNotFound)
return;
count += 1;
if (count == 4) {
[result appendString:@" "];
count = 0;
}
[result appendString:substring];
}];
return result;
}
@end
尝试使用此测试字符串:
NSString *string = @"ab 132487 387 e e e ";
NSLog(@"%@", [string stringByFormattingAsCreditCardNumber]);
该方法适用于非BMP字符(即表情符号)并处理现有的空格。
答案 1 :(得分:5)
你应该这样做:
- (NSString *)resetCardNumberAsVisa:(NSString*)originalString {
NSMutableString *resultString = [NSMutableString string];
for(int i = 0; i<[originalString length]/4; i++)
{
NSUInteger fromIndex = i * 4;
NSUInteger len = [originalString length] - fromIndex;
if (len > 4) {
len = 4;
}
[resultString appendFormat:@"%@ ",[originalString substringWithRange:NSMakeRange(fromIndex, len)]];
}
return resultString;
}
<强>更新强>
您的代码将在第一个插入空间字符:
这是您的originalString
:
Text: 123412341234
Location: 012345678901
根据您的代码,在第一个插入空格字符时,您将插入&#34; 1&#34; (位置为4 )
之后,你的字符串是:
Text: 1234 12341234
Location: 0123456789012
所以,你看,现在你必须在 9 (9%4 != 0)的位置插入第二个空间字符
希望您自己修复代码!
答案 2 :(得分:3)
您的代码非常接近,但该方法的更好的语义是为任何给定的输入字符串返回新的NSString
:
-(NSString *)formatStringAsVisa:(NSString*)aNumber
{
NSMutableString *newStr = [NSMutableString new];
for (NSUInteger i = 0; i < [aNumber length]; i++)
{
if (i > 0 && i % 4 == 0)
[newStr appendString:@" "];
unichar c = [aNumber characterAtIndex:i];
[newStr appendString:[[NSString alloc] initWithCharacters:&c length:1]];
}
return newStr;
}
答案 3 :(得分:1)
here的代码段可以做你想做的事情:
- (NSString *)insertSpacesEveryFourDigitsIntoString:(NSString *)string
andPreserveCursorPosition:(NSUInteger *)cursorPosition
{
NSMutableString *stringWithAddedSpaces = [NSMutableString new];
NSUInteger cursorPositionInSpacelessString = *cursorPosition;
for (NSUInteger i=0; i<[string length]; i++) {
if ((i>0) && ((i % 4) == 0)) {
[stringWithAddedSpaces appendString:@" "];
if (i < cursorPositionInSpacelessString) {
(*cursorPosition)++;
}
}
unichar characterToAdd = [string characterAtIndex:i];
NSString *stringToAdd =
[NSString stringWithCharacters:&characterToAdd length:1];
[stringWithAddedSpaces appendString:stringToAdd];
}
return stringWithAddedSpaces;
}
答案 4 :(得分:0)
基于Droppy的swift3
func codeFormat(_ code: String) -> String {
let newStr = NSMutableString()
for i in 0..<code.characters.count {
if (i > 0 && i % 4 == 0){
newStr.append(" ")
}
var c = (code as NSString).character(at: i)
newStr.append(NSString(characters: &c, length: 1) as String)
}
return newStr as String
}
答案 5 :(得分:0)
请确保您的字符串长度应为4。
该解决方案将首先插入右侧。
- (NSString*) fillWhiteGapWithString:(NSString*)source
{
NSInteger dl = 4;
NSMutableString* result = [NSMutableString stringWithString:source];
for(NSInteger cnt = result.length - dl ; cnt > 0 ; cnt -= dl)
{
[result insertString:@" " atIndex:cnt];
}
return result;
}