如何循环NSString N次?

时间:2015-11-22 04:17:20

标签: ios objective-c nsstring

如何将字符串循环9次? (z x c v b n z x c)

NSString *fl = @"zxcvbn";

2 个答案:

答案 0 :(得分:1)

这是一个快速而又脏的片段:

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

答案 1 :(得分:0)

除了使用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') { ... } } 来获取字符值:

<form method="post" id="filterform">
       <select multiple name="category[]" id="category" class="select">
           <option value="10">Men Foreigner</option>
           <option value="11">Women Foreigner</option>
        </select>

        <select multiple name="hair[]" id="hair" class="select">
           <option value="Black">Black</option>
           <option value="Blond">Blond</option>
        </select>
</form>