我的 for 循环似乎没有按照我的要求做。如果它不等于空格,我正在尝试进行计数,如果它等于空格,我正在尝试删除另一个 for 循环。两者似乎都不起作用,因为我的 NSLOG 告诉我它仍在计算空格,也没有删除它们。任何帮助将不胜感激。
NSMutableArray *a1 = [[NSMutableArray alloc] init];
// Take address and break into array by colon
if ([str rangeOfString:@"::"].location == NSNotFound)
{
NSLog(@"Double Colon not found");
}
else
{
a1 = [[display.text componentsSeparatedByString:@":"]mutableCopy];
//Separate string by colon
NSLog(@"Separated string by colon: %@",a1);
//count non blank segments
for (item in a1) {
if (item != @""){
numofSeg = numofSeg + 1;
needSeg = 8 - numofSeg;
NSLog(@"%d",needSeg);
}
}
while (needSeg > 0) {
NSUInteger index = [a1 indexOfObject:@""];
if (index != NSNotFound) {
[a1 insertObject:@"0000" atIndex:index];
needSeg = needSeg - 1;
}
}
for (item in a1) {
if (item == @"") {
//remove blank spaces from list
[a1 removeObject:item];
}
}
}
//Join objects back into a string separted by colon.
NSString *join = [a1 componentsJoinedByString:@":"];
NSLog(@"Join array into String: %@",join);
下面输出。当我将阵列重新连接在一起时,它应该等于 8 个段,但只有 6 个显示。这告诉我,当我分隔双冒号时,它正在计算两个空格。
2012-10-01 19:50:52.430 IPv6[1287:c07] Separated string by colon: (
2002,
0000,
"",
""
)
2012-10-01 19:50:52.432 IPv6[1287:c07] 7
2012-10-01 19:50:52.433 IPv6[1287:c07] 6
2012-10-01 19:50:52.434 IPv6[1287:c07] 5
2012-10-01 19:50:52.435 IPv6[1287:c07] 4
2012-10-01 19:50:52.435 IPv6[1287:c07] Join array into String: 2002:0000:0000:0000:0000:0000::