xcode 如何查看字符串是否包含子字符串(objective-c)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8549898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:46:44  来源:igfitidea点击:

how to see if a string contains a substring (objective-c)

objective-cxcodestringcomparesubstring

提问by user973985

I want to see if a string, which is a title of a post on the apple rss news feed contains a substring, e.g. "Steve" or "Jobs". I organized the posts into a uitableview.

我想查看一个字符串(Apple RSS 新闻提要上的帖子标题)是否包含子字符串,例如“Steve”或“Jobs”。我将帖子组织成一个 uitableview。

So there WAS a post which had Steve or Jobs in its title so I used this to check:

所以有一篇文章的标题是史蒂夫或乔布斯,所以我用它来检查:

   if ([[entry title] localizedCaseInsensitiveCompare:@"Steve"] == NSOrderedSame ||        [[entry title] localizedCaseInsensitiveCompare: @"Jobs"] == NSOrderedSame) {

    NSLog(@"Comparism of Steve Jobs");
    cell.imageView.image = [UIImage imageNamed:@"steve.png"];
}

But it was never called, entry is an RSSItem class which contains the title - the entry and its title is not my problem, I have checked. My comparism is the problem. How do i compare

但它从未被调用过,条目是一个包含标题的 RSSItem 类 - 条目及其标题不是我的问题,我已经检查过了。我的比较是问题所在。我如何比较

UPDATE!

更新!

Ok here is the code:

好的,这是代码:

NSRange range = [[[cell textLabel] text] rangeOfString:@"Steve" options:NSCaseInsensitiveSearch];

if (range.location != NSNotFound) 
{
    cell.imageView.image = [UIImage imageNamed:@"steve.png"];


}

I have tried it other peoples way also but SAME RESULT:

我也尝试过其他人的方式,但结果相同:

some cells have their imageView as steve.png even though their title doesnt contain steve jobs. Weird??? I scroll down and when I go back up all the dequeued cells which are reallocated and initialized have the steve jobs picture. At starting when i open the app some cells which don't have steve in their title DO HAVE THE IMAGE, and then the above happens.

一些单元格的 imageView 为 steve.png,即使它们的标题不包含 steve jobs。奇怪的???我向下滚动,当我返回所有重新分配和初始化的出列单元格时,会显示史蒂夫·乔布斯的图片。在开始时,当我打开应用程序时,一些标题中没有史蒂夫的单元格确实有图像,然后就会发生上述情况。

My surrounding code IF NEEDED:

如果需要,我的周围代码:

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:@"UITableViewCell"]
            autorelease];
}


tableView.autoresizingMask = 5;
tableView.autoresizesSubviews = YES;
cell.autoresizingMask = 5;
cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, 20, 20);
RSSItem *item = [[channel items] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[item title]];
NSMutableString *string = [[NSMutableString alloc] initWithString:[[cell textLabel] text]];

if (string.length > 46) {
    cell.textLabel.numberOfLines = 2;
    UILineBreakMode lineBreak = UILineBreakModeClip;
    cell.textLabel.lineBreakMode = lineBreak;

}

[string release];

tableView.backgroundColor = [UIColor darkGrayColor];
cell.textLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size: 12.0];
cell.backgroundColor = [UIColor whiteColor];

NSRange range = [[[cell textLabel] text] rangeOfString:@"Steve" options:NSCaseInsensitiveSearch];

if (range.location != NSNotFound) 
{
    cell.imageView.image = [UIImage imageNamed:@"steve.png"];


    }



return cell;

    }

采纳答案by Hot Licks

"then when i scroll back up all the recycled cells have the steve jobs image"

“然后当我向上滚动时,所有回收的单元格都有史蒂夫·乔布斯的图像”

That's because you're not resetting the image in the recycled cells. (Try adding

那是因为您没有重置回收单元中的图像。(尝试添加

else { 
    cell.imageView.image = nil; 
}

at an appropriate place.)

在适当的地方。)

回答by Josh Rosen

NSString-rangeOfString returns an NSRange, which can be checked for the "not found" case.

NSString-rangeOfString 返回一个 NSRange,可以检查“未找到”的情况。

if ([@"Some awesome string." rangeOfString:@"awesome"].location != NSNotFound)
{
  // awesome is in 'Some awesome string.'
}
else 
{
 // awesome is not in 'Some awesome string.' 
}

回答by Hot Licks

You're comparing the entire string, and "Steve Jobs" won't match either "Steve" or "Jobs". You probably want to use rangeOfString:@"Steve" options:NSCaseInsensitiveSearch, or some such.

您正在比较整个字符串,“史蒂夫乔布斯”不会匹配“史蒂夫”或“乔布斯”。您可能想使用rangeOfString:@"Steve" options:NSCaseInsensitiveSearch,或一些类似的。

回答by Fatima Arshad

If you are on iOS8 you can do this now as:

如果您使用的是 iOS8,您现在可以这样做:

  NSString *stringToSearch = @"Steve";
  if ([stringToSearch containsString:@"Steve"])
  {
      NSLog(@"String contains Steve!!!");
  } 
  else 
  {
      NSLog(@"String does not contain Steve!!!");
  }