How to see if an NSString starts with a certain other string?

IosObjective CIphoneNsstringNsmutablestring

Ios Problem Overview


I am trying to check to see if a string that I am going to use as URL starts with http. The way I am trying to check right now doesn't seem to be working. Here is my code:

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

Any ideas?

Ios Solutions


Solution 1 - Ios

Try this: if ([myString hasPrefix:@"http"]).

By the way, your test should be != NSNotFound instead of == NSNotFound. But say your URL is ftp://my_http_host.com/thing, it'll match but shouldn't.

Solution 2 - Ios

I like to use this method:

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

or even easier:

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

Solution 3 - Ios

If you're checking for "http:" you'll probably want case-insensitive search:

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

Solution 4 - Ios

Swift version:

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

Solution 5 - Ios

This is my solution to the problem. It will remove the letters that are not necessary and is case insensitive.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobView Question on Stackoverflow
Solution 1 - IosCyrilleView Answer on Stackoverflow
Solution 2 - IosJonasGView Answer on Stackoverflow
Solution 3 - IosbobicsView Answer on Stackoverflow
Solution 4 - IosRichardView Answer on Stackoverflow
Solution 5 - IosHackmodfordView Answer on Stackoverflow