【问题标题】:how to see if a string contains a substring (objective-c)如何查看字符串是否包含子字符串(objective-c)
【发布时间】:2012-01-22 21:48:08
【问题描述】:

我想查看一个字符串,它是苹果 rss 新闻提要上帖子的标题,是否包含子字符串,例如“史蒂夫”或“乔布斯”。我将帖子组织成一个 uitableview。

所以有一个帖子的标题中有史蒂夫或乔布斯,所以我用这个来检查:

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

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

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

更新!

好的,代码如下:

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

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


}

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

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

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

  -(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;

    }

【问题讨论】:

  • 请给出几个错误匹配“Steve”的单元格的文本。
  • 您是否要从不是“Steve”的回收单元格中删除图像?在我看来它不像。
  • 与史蒂夫错误匹配的几个单元格的文本:苹果董事会声明一天 1 亿次 OSX Lion 下载
  • 然后当我向上滚动时,所有回收的单元格都有史蒂夫·乔布斯的形象
  • “然后当我向上滚动所有回收的单元格时,所有的回收单元都有史蒂夫·乔布斯的图像”——那是因为你没有重置回收单元中的图像。 (尝试在适当的位置添加else { cell.imageView.image = nil; }。)

标签: objective-c xcode string compare substring


【解决方案1】:

您正在比较整个字符串,并且“Steve Jobs”不会匹配“Steve”或“Jobs”。你可能想使用rangeOfString:@"Steve" options:NSCaseInsensitiveSearch,或类似的。

【讨论】:

  • 但这不会告诉我史蒂夫是否位于字符串中
  • 返回的 NSRange 与我想要的相差无几>
  • 它完全符合您的要求。如果您阅读文档,如果不包含该字符串,它将返回 {NSNotFound, 0}NSRange
【解决方案2】:

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.' 
}

【讨论】:

  • +1 使用 options:NSCaseInsensitiveSearch 版本可以实现 OP 的(明显的)不区分大小写搜索的目标。
  • 由于某种原因,即使这不正确,这种比较也会被调用检查我更新的问题
【解决方案3】:

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

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

else { 
    cell.imageView.image = nil; 
}

在适当的地方。)

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-12-20
      • 2011-11-09
      • 2013-05-18
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多