如何循环 NSString N 次?
How do I loop through NSString N number of times?
如何将字符串循环 9 次? (z x c v b n z x c)
NSString *fl = @"zxcvbn";
这是一个快速而粗略的片段:
NSString *string = @"abcdef";
NSString *letter = nil;
int n = 9;
for (int index = 0; index < n; index++) {
// start over if it's more than the length
int currentIndex = index % string.length;
letter = [string substringWithRange:NSMakeRange(currentIndex, 1)];
NSLog(@"letter: %@", letter);
}
如果你想要一个带有详细解释的低级示例check this out。
除了使用substringWithRange
(其中returns一个NSString
),还可以使用characterAtIndex
获取字符值:
NSInteger n = 9;
for (NSInteger index = 0; index < n; index++) {
unichar character = [fl characterAtIndex:index % fl.length];
// do something with `character`, e.g.
//
// if (character == 'z') { ... }
}
如何将字符串循环 9 次? (z x c v b n z x c)
NSString *fl = @"zxcvbn";
这是一个快速而粗略的片段:
NSString *string = @"abcdef";
NSString *letter = nil;
int n = 9;
for (int index = 0; index < n; index++) {
// start over if it's more than the length
int currentIndex = index % string.length;
letter = [string substringWithRange:NSMakeRange(currentIndex, 1)];
NSLog(@"letter: %@", letter);
}
如果你想要一个带有详细解释的低级示例check this out。
除了使用substringWithRange
(其中returns一个NSString
),还可以使用characterAtIndex
获取字符值:
NSInteger n = 9;
for (NSInteger index = 0; index < n; index++) {
unichar character = [fl characterAtIndex:index % fl.length];
// do something with `character`, e.g.
//
// if (character == 'z') { ... }
}