Search code examples
iphoneobjective-csubstringnsmutablestring

How to selectively trim an NSMutableString?


I would like to know how to selectively trim an NSMutableString. For example, if my string is "MobileSafari_2011-09-10-155814_Jareds-iPhone.plist", how would I programatically trim off everything except the word "MobileSafari"?

Note : Given the term programatically above, I expect the solution to work even if the word "MobileSafari" is changed to "Youtube" for example, or the word "Jared's-iPhone" is changed to "Angela's-iPhone".

Any help is very much appreciated!


Solution

  • TESTED CODE: 100% WORKS

    NSString *inputString=@"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";
    
    NSArray *array= [inputString componentsSeparatedByString:@"_"];
    
    if ([array count]>0) {
    
        NSString *resultedString=[array objectAtIndex:0];
    
    
        NSLog(@" resultedString IS - %@",resultedString);
    
    
    
    }
    

    OUTPUT:

    resultedString IS - MobileSafari