NSString and string contains

  • Hello,
    I'm fairly new to Cocoa, so please excuse me if I'm not using the
    right terminology.

    I have an NSArray which contains String values and I want to loop
    though it and determine if any of those string contain words I'm
    looking for. I have tried but have been unsuccessful.

    Example...

    unsigned arrayCount = [array count];
    for(unsigned j = 0; j < arrayCount; j++)
      {
    id obj;
    obj = [array objectAtIndex:j];
    /* Here is where I'm having trouble
    I know this is not real :-)
    if ( [obj inStr:@"home"])
    {
      NSLog(@"I found home, %@", obj);
    }
    */
    }

    I hope the example helps, thanks,
    tom
  • On Mar 2, 2008, at 12:31 PM, Tom Jones wrote:

    > id obj;
    > obj = [array objectAtIndex:j];
    > /* Here is where I'm having trouble
    > I know this is not real :-)
    > if ( [obj inStr:@"home"])

    http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes
    /NSString_Class/Reference/NSString.html#/

    /apple_ref/doc/uid/20000154-rangeOfString_options_range_

    --
    Seth Willits
  • Hi, Tom,

    You probably want to use enumerators and something like the following
    code:

    > NSEnumerator *e = [array objectEnumerator];
    > NSString *string;
    >
    > while ((string = [e nextObject])) {
    > NSRange range = [string rangeOfString:@"home"];
    > if (range.location != NSNotFound) {
    > NSLog(@"I found home in the string %@", string);
    > }
    > }

    The enumerator does what your for-loop does, except that it gives you
    the objects rather than making you use -objectAtIndex:.  Additionally,
    the NSRange struct gives you a location, from -rangeOfString:, where
    the found string -- if it isn't the location NSNotFound, which may be
    self-explanatory -- where the string was found, and thus tells you
    whether it was found in the first place.

    Cheers,
    Andrew

    On Mar 2, 2008, at 12:31 PM, Tom Jones wrote:

    > Hello,
    > I'm fairly new to Cocoa, so please excuse me if I'm not using the
    > right terminology.
    >
    > I have an NSArray which contains String values and I want to loop
    > though it and determine if any of those string contain words I'm
    > looking for. I have tried but have been unsuccessful.
    >
    > Example...
    >
    > unsigned arrayCount = [array count];
    > for(unsigned j = 0; j < arrayCount; j++)
    > {
    > id obj;
    > obj = [array objectAtIndex:j];
    > /* Here is where I'm having trouble
    > I know this is not real :-)
    > if ( [obj inStr:@"home"])
    > {
    > NSLog(@"I found home, %@", obj);
    > }
    > */
    > }
    >
    >
    > I hope the example helps, thanks,
    > tom
  • On Mar 2, 2008, at 10:44 PM, Andrew Merenbach wrote:

    > You probably want to use enumerators and something like the
    > following code:

    ...and if you're on 10.5 (or later - even if that's kind of unlikely
    at this point) you'd use the new FAST enumerator:

    for (NSString *string in array)
    {
    // Do stuff with the string
    }

    j o a r
  • Or if your're on Leopard using the new enumerator:

    NSString *searchFor = @"home";
    NSRange range;
    for (NSString *string in stringList)
    {
        range = [word rangeOfString:searchFor];
        if (range.location != NSNotFound)
        {
          NSLog (@"Yay! '%@' found in '%@'.", searchFor, string);
        }
    }

    Correct me if I'm wrong.

    _____________
      Johannes H.

    On Mar 2, 2008, at 10:44 PM, Andrew Merenbach wrote:

    > Hi, Tom,
    >
    > You probably want to use enumerators and something like the
    > following code:
    >
    >> NSEnumerator *e = [array objectEnumerator];
    >> NSString *string;
    >>
    >> while ((string = [e nextObject])) {
    >> NSRange range = [string rangeOfString:@"home"];
    >> if (range.location != NSNotFound) {
    >> NSLog(@"I found home in the string %@", string);
    >> }
    >> }
    >
    > The enumerator does what your for-loop does, except that it gives
    > you the objects rather than making you use -objectAtIndex:.
    > Additionally, the NSRange struct gives you a location, from -
    > rangeOfString:, where the found string -- if it isn't the location
    > NSNotFound, which may be self-explanatory -- where the string was
    > found, and thus tells you whether it was found in the first place.
    >
    > Cheers,
    > Andrew
    >
    >
    >
    > On Mar 2, 2008, at 12:31 PM, Tom Jones wrote:
    >
    >> Hello,
    >> I'm fairly new to Cocoa, so please excuse me if I'm not using the
    >> right terminology.
    >>
    >> I have an NSArray which contains String values and I want to loop
    >> though it and determine if any of those string contain words I'm
    >> looking for. I have tried but have been unsuccessful.
    >>
    >> Example...
    >>
    >> unsigned arrayCount = [array count];
    >> for(unsigned j = 0; j < arrayCount; j++)
    >> {
    >> id obj;
    >> obj = [array objectAtIndex:j];
    >> /* Here is where I'm having trouble
    >> I know this is not real :-)
    >> if ( [obj inStr:@"home"])
    >> {
    >> NSLog(@"I found home, %@", obj);
    >> }
    >> */
    >> }
    >>
    >>
    >> I hope the example helps, thanks,
    >> tom

  • Yep, I would've suggested fast enumeration, as I use them myself
    regularly, except that a beginner might (or might not) find the
    traditional paradigm easier.

    Cheers,
    Andrew

    On Mar 2, 2008, at 2:01 PM, j o a r wrote:

    >
    > On Mar 2, 2008, at 10:44 PM, Andrew Merenbach wrote:
    >
    >> You probably want to use enumerators and something like the
    >> following code:
    >
    >
    > ...and if you're on 10.5 (or later - even if that's kind of unlikely
    > at this point) you'd use the new FAST enumerator:
    >
    > for (NSString *string in array)
    > {
    > // Do stuff with the string
    > }
    >
    > j o a r
    >
    >
  • On Mar 2, 2008, at 12:31 PM, Tom Jones wrote:

    > I have an NSArray which contains String values and I want to loop
    > though it and determine if any of those string contain words I'm
    > looking for. I have tried but have been unsuccessful.

    Predicates are great for this.  They're a Foundation facility for
    performing queries on collections.

      - (NSArray *)itemsInCollection:(NSArray *)collection containingWord:
    (NSString *)word {
          NSPredicate *containsWordPredicate = [NSPredicate @"SELF
    CONTAINS[cd] %@", word];

          return [collection
    filteredArrayUsingPredicate:containsWordPredicate];
      }

    This will return a new collection containing all items in the passed-
    in collection that themselvs contain the given word, and it will do
    the comparison in a case-insensitive and diacritic-insensitive
    fashion.  (Thus if word is @"resume" then @"résumé" and @"Resume"
    would be considered matches.)

    In general, any time you can express something as a query rather than
    as explicit iteration, it's a win -- whether you're talking about
    Cocoa or any other framework.  This is because it lets the framework
    itself determine the best way to achieve the results, which can change
    over time.

      -- Chris
  • On 2 Mar '08, at 1:44 PM, Andrew Merenbach wrote:

    > You probably want to use enumerators and something like the
    > following code:

    That's debatable. Personally, I hate using NSEnumerator because it's
    slow. (Not only does it create an autoreleased enumerator object, but
    most of the standard collection enumerators copy the collections they
    enumerate over.)

    On the other hand, using fast enumeration via the "for...in..." syntax
    in ObjC-2 is a total win. Not only is the syntax clear, but the
    implementation is very efficient (faster than -objectAtIndex, much
    faster than NSEnumerator.)

    —Jens
previous month march 2008 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today