sincronizados - limitar correos iphone 7
¿Cómo ver si un NSString comienza con una cierta otra cadena? (5)
Estoy tratando de verificar si una cadena que voy a usar como URL comienza con http. La forma en que estoy tratando de verificar en este momento no parece estar funcionando. Aquí está mi código:
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];
¿Algunas ideas?
Esta es mi solución al problema. Eliminará las letras que no son necesarias y no distingue entre mayúsculas y minúsculas.
- (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;
}
Me gusta usar este método:
if ([[temp substringToIndex:4] isEqualToString:@"http"]) {
//starts with http
}
o incluso más fácil:
if ([temp hasPrefix:@"http"]) {
//do your stuff
}
Pruebe esto: if ([myString hasPrefix:@"http"])
.
Por cierto, tu prueba debería ser != NSNotFound
lugar de == NSNotFound
. Pero supongamos que su URL es ftp://my_http_host.com/thing
, coincidirá pero no debería.
Si está buscando "http:" probablemente querrá buscar mayúsculas y minúsculas:
NSRange prefixRange =
[temp rangeOfString:@"http"
options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];
if (prefixRange.location == NSNotFound)
Versión Swift:
if line.hasPrefix("#") {
// checks to see if a string (line) begins with the character "#"
}