2011-10-28 6 views
138

URLとして使用する文字列がhttpで始まるかどうかを確認しようとしています。私が現在チェックしようとしている方法は機能していないようです。ここに私のコードです:NSStringが特定の他の文字列で始まるかどうかを確認するには?

NSMutableString *temp = [[NSMutableString alloc] initWithString:@"http://"]; 
if ([businessWebsite rangeOfString:@"http"].location == NSNotFound){ 
    NSString *temp2 = [[NSString alloc] init]; 
    temp2 = businessWebsite; 
    [temp appendString:temp2]; 
    businessWebsite = temp2; 
    NSLog(@"Updated BusinessWebsite is: %@", businessWebsite); 
} 

[web setBusinessWebsiteUrl:businessWebsite]; 

アイデア?

答えて

310

お試しください:if ([myString hasPrefix:@"http"])

ところで、あなたのテストは、== NSNotFoundの代わりに!= NSNotFoundである必要があります。しかし、あなたのURLはftp://my_http_host.com/thingだと言えば、一致しますが、そうではありません。あなたがチェックしている場合は

if ([temp hasPrefix:@"http"]) { 
    //do your stuff 
} 
+0

うん。私は!=以前のことに気づいていたはずですが、結局は働いていたhasPrefixでした。アドバイスをいただきありがとうございます、私はあなたが私を可能にするとすぐに正しい答えとしてマークします。 – Rob

22

私はこの方法を使用するように "http:" あなたはおそらく、大文字と小文字を区別しない検索をお勧めします:

NSRange prefixRange = 
    [temp rangeOfString:@"http" 
       options:(NSAnchoredSearch | NSCaseInsensitiveSearch)]; 
if (prefixRange.location == NSNotFound) 
+1

それも良いですね。コメントはありがたいです – Rob

+2

一時文字列が5文字未満の場合、クラッシュします。インデックスは0から始まるので、これは良い答えではありません。また、この例では、文字数の不一致があります。「http」は5文字ではありません。大文字小文字の区別も考慮する必要があります。 – Daniel

+0

ダニエル何と言っているのですか?なぜ5?これはNSArrayではありません...インデックス4は5番目ではなく4番目の文字です!そしてあなたはHttpやhTtPを見たことがありますか?大文字と小文字は区別されません。また、文字列が4文字より短い文字列ではなく、文字列がhttpで始まるかどうかを確認することも問題でした。 hasPrefix:より良いですが、これはちょうど同様に動作します。 – JonasG

5

if ([[temp substringToIndex:4] isEqualToString:@"http"]) { 
    //starts with http 
} 

またはさらに簡単:

0

これは私のこの問題に対する解決策です。それは不要で大文字小文字を区別しない文字を削除します。

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
    return [self generateSectionTitles]; 
} 

-(NSArray *)generateSectionTitles { 

    NSArray *alphaArray = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil]; 

    NSMutableArray *sectionArray = [[NSMutableArray alloc] init]; 

    for (NSString *character in alphaArray) { 



     if ([self stringPrefix:character isInArray:self.depNameRows]) { 
      [sectionArray addObject:character]; 
     } 

    } 

    return sectionArray; 

} 

-(BOOL)stringPrefix:(NSString *)prefix isInArray:(NSArray *)array { 

    for (NSString *str in array) { 

     //I needed a case insensitive search so [str hasPrefix:prefix]; would not have worked for me. 
     NSRange prefixRange = [str rangeOfString:prefix options:(NSAnchoredSearch | NSCaseInsensitiveSearch)]; 
     if (prefixRange.location != NSNotFound) { 
      return TRUE; 
     } 

    } 

    return FALSE; 

} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { 

    NSInteger newRow = [self indexForFirstChar:title inArray:self.depNameRows]; 
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:newRow inSection:0]; 
    [tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; 

    return index; 
} 

// Return the index for the location of the first item in an array that begins with a certain character 
- (NSInteger)indexForFirstChar:(NSString *)character inArray:(NSArray *)array 
{ 
    NSUInteger count = 0; 
    for (NSString *str in array) { 

     //I needed a case insensitive search so [str hasPrefix:prefix]; would not have worked for me. 
     NSRange prefixRange = [str rangeOfString:character options:(NSAnchoredSearch | NSCaseInsensitiveSearch)]; 
     if (prefixRange.location != NSNotFound) { 
      return count; 
     } 
     count++; 
    } 
    return 0; 
} 
2

スウィフト版:それだった

if line.hasPrefix("#") { 
    // checks to see if a string (line) begins with the character "#" 
} 
+0

なぜこれがダウン投票されたのか分かりません...これはこれを行う簡単な迅速な方法です。ほとんどの新しいiOS開発者はおそらくここからSwiftを使用しようとしており、OPはObjective-Cの回答だけが要求されたとは決して言わなかった。 – Richard

+0

「これがなぜ投票されなかったのかわかりません」 - おそらく構文が間違っているからでしょうか? if if line.hasPrefix( "prefix") '{}' –

+0

より簡単な構文を指摘してくれてありがとう、if文の周りに()を置くのは悪い構文ではありません。私たちの古いタイマーのいくつかについては、それはより明確に読んで、まったく同じように動作します。 'if(line.hasPrefix("# ")){}'も同様に動作します。 – Richard

関連する問題