对于objective-c,我如何找出两个给定字母之间的字母。例如,对于两个字母A和D,我应该得到A,B,C,D。
答案 0 :(得分: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
希望这会有所帮助。
答案 1 :(得分:2)
最短的代码是:
for (char c = 'a'; c <= 'd'; c++)
NSLog(@"char = %c", c);
答案 2 :(得分: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]);
}