2011-07-22 4 views

Répondre

3

Une façon est de briser la chaîne vers le haut dans des premières lignes:

NSArray *lines = [myString componentsSeparatedByString:@"\n"]; 

Et puis recherchez à travers les lignes:

for (NSString *line in lines) { 
    NSRange *range = [line rangeOfString:@"Y"]; 
    if (range.length > 0) { 
     // do something 
    } 
} 
0

Une alternative à la réponse de Caleb et si vous utilisez iOS 4.0 ou plus tard, vous pouvez utiliser enumerateLinesUsingBlock.

__block NSString * theLine; 
[lines enumerateLinesUsingBlock:^(NSString * line, BOOL * stop){ 
    NSRange range = [line rangeOfString:@"Y"]; 
    if (range.location != NSNotFound) { 
     theLine = [line retain]; 
     *stop = YES; 
    } 
}]; 

/* Use `theLine` for something */ 
[theLine release]; // Relinquish ownership 
+0

Hey @Deepak: J'ai besoin peu des conseils sur certaines choses. Peux-tu m'aider ? – Legolas

+0

http://stackoverflow.com/questions/6792069/i-need-expert-advice-on-this-implementation – Legolas

0

Vous pouvez faire quelque chose comme ça

- (NSString *)LineTextFinder:(NSString*)inData Serach:(char)inSearch LineDelimiter:(char)inChar 
{ 
    int lineStart = 0; 
    int lineEnd =0; 

    int i = 0; 
    BOOL found = NO; 

    while (1) 
    { 
     if(i > [inData length]) 
     { 
      lineStart = lineEnd; 
      lineEnd = i; 

      break; 
     } 
     if (inChar == [inData characterAtIndex:i]) 
     { 
      lineStart = lineEnd; 
      lineEnd = i; 


      //we can brack only when we travell end of found line 
      if (found) 
      { 
       break; 
      } 
     } 

     if(inSearch == [inData characterAtIndex:i]) 
     { 
      found = YES; 
     } 
     i++; 
    } 


    NSString *returnStr = nil; 
    if ((found)) 
    { 
     NSRange splitRange = {lineStart,(lineEnd-lineStart)}; 
     returnStr = [inData substringWithRange:splitRange]; 
    } 

    return returnStr; 
} 
Questions connexes