将字符串与数组进行比较

时间:2011-04-07 23:51:57

标签: objective-c xcode ios4

我正试图从标签中获取一个单词并将其与文本文件进行比较,但出于某种原因,无论我做什么,我都会得到“否”。代码有问题吗?

wordS1 = [[NSBundle mainBundle] pathForResource:@"c" ofType:@"txt"];
wordS2 = [NSString stringWithContentsOfFile:wordS1 encoding:NSUTF8StringEncoding error:nil];        
wordArray = [wordS2   componentsSeparatedByString:@"\n"];


for (compare = 0 ; compare != 58; compare++) 
{

    if ( [word.text isEqualToString:[wordArray objectAtIndex:compare]]) 
    {
        NSLog(@"Yes");    

    }
    else
    {
        NSLog(@"No");
    }


}

1 个答案:

答案 0 :(得分:0)

我的猜测是文本文件中的字符串中有一些不需要的空格。尝试将isEqualToString更改为hasPrefix

编辑:这实际上不是最好的方法。在这里查看从字符串Collapse sequences of white space into a single character and trim string

中删除空格