How to know if an NSString contains a string?

  • I guess this is one of those things I'll smack myself for, but I can't
    find a good way to do it:

    How do I know if a string contains another string? Something like
    myBool = [myString containsString:@"bla"]  is what I need.

    Any pointers?

    Thanks,

    Jan Van Boghout
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • > How do I know if a string contains another string? Something like
    > myBool = [myString containsString:@"bla"]  is what I need.
    >

    NSRange aRange = [myString rangeOfString:@"bla"];
    if (aRange.location ==NSNotFound) {
      NSLog(@"string not found");
    } else {
      NSLog(@"string was at index %d ",aRange.location);
    }
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Friday, January 10, 2003, at 01:52  PM, Jan Van Boghout wrote:

    > I guess this is one of those things I'll smack myself for, but I can't
    > find a good way to do it:
    >
    > How do I know if a string contains another string? Something like
    > myBool = [myString containsString:@"bla"]  is what I need.
    >
    > Any pointers?

    Well, here's how I do it:

    if ([myString rangeOfString:@"bla"].location != NSNotFound) {
      //The string has been found
    }

    There may be an easier way, but this works fine for me.

    -Jan Van Tol
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • > How do I know if a string contains another string? Something like
    > myBool = [myString containsString:@"bla"]  is what I need.

    What about simply looking for a substring, like this:

    myBool = !([myString rangeOfString:@"bla"].location == NSNotFound);

    Jeff
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Friday, January 10, 2003, at 07:52  PM, Jan Van Boghout wrote:

    > I guess this is one of those things I'll smack myself for, but I can't
    > find a good way to do it:
    >
    > How do I know if a string contains another string? Something like
    > myBool = [myString containsString:@"bla"]  is what I need.
    >
    > Any pointers?

    How about

    NSRange subStrRange = [myString  rangeOfString:@"bla"];
    myBool =  (subStrRange.location != NSNotFound);

    BTW, I find that "Cocoa Broswer" is invaluable for finding things like
    this.

    http://homepage2.nifty.com/hoshi-takanori/cocoa-browser/

    _murat
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • > I guess this is one of those things I'll smack myself for, but I can't
    > find a good way to do it:
    > How do I know if a string contains another string? Something like
    > myBool = [myString containsString:@"bla"]  is what I need.

    rangeOfString:... and friends return the range in which the argument
    string occurs in the receiver.
    If the returned range has length 0, then the string does not occur in
    the receiver.
    Note that due to design of Unicode, it is possible that length of the
    returned range is not equal to the length of the target string. If you
    need to know where the target string occurs, always use the returned
    range.
    Ali
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Thanks for the replies everyone. I had already put together a thing
    with stringComponents but this is much better :)

    > I guess this is one of those things I'll smack myself for, but I can't
    > find a good way to do it:
    >
    > How do I know if a string contains another string? Something like
    > myBool = [myString containsString:@"bla"]  is what I need.
    >
    > Any pointers?
    >
    > Thanks,
    >
    >
    > Jan Van Boghout
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month january 2003 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