-3

对于objective-c,我如何找出两个给定字母之间的字母。例如,对于两个字母 A 和 D,我应该得到 A、B、C、D。

4

3 回答 3

2

你可以试试这段代码:

NSString *start = @"A";
NSString *end = @"D";
unichar startChar = [start characterAtIndex:0];
unichar endChar = [end characterAtIndex:0];
for (unichar c = startChar; c <= endChar; c++)
{
    NSLog(@"%c",c);
}

结果:

2012-12-17 19:01:36.259 Test[7492:907] A
2012-12-17 19:01:36.263 Test[7492:907] B
2012-12-17 19:01:36.264 Test[7492:907] C
2012-12-17 19:01:36.265 Test[7492:907] D

希望这可以帮助。

于 2012-12-17T11:03:19.057 回答
2

最短的代码是:

for (char c = 'a'; c <= 'd'; c++)
    NSLog(@"char = %c", c);
于 2012-12-17T11:36:02.143 回答
1

第一的

unichar StartChar = [@"R" characterAtIndex:0];
unichar EndChar = [@"X" characterAtIndex:0];
for (unichar Result = StartChar; c <= EndChar; c++)
{
    NSLog(@"%c",Result);
}

第二

NSString *string = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int startIndex = [string rangeOfString:@"A"].location;
NSString *subString = [string substringFromIndex:startIndex];

int EndIndex = [subString rangeOfString:@"H"].location;

NSString*result = [subString substringToIndex:EndIndex+1];
NSLog(@"%@",result);  

第三

NSArray *string = [[UILocalizedIndexedCollation currentCollation] sectionTitles];
int startIndex = [string indexOfObject:@"D"];
int EndIndexIndex = [string indexOfObject:@"I"];

for (int i=startIndex; i<=EndIndexIndex; i++)
{
    NSLog(@"%@",[string objectAtIndex:i]);
}
于 2012-12-17T11:54:15.317 回答