Finding Substrings in Objective-C

  • Twitter
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
November 3rd, 2008 Posted by: - posted under:Snippets

It’s times like this, that I miss ruby.

I’m checking a url to see if it has a substring. It would be so easy if this was ruby:

absolute_url.match(/my regex/).any?

In Objective C, you have to use rangeOfString which returns a range. If I were to run this on the string “the quick brown fox” with an argument of “brown” it would return {10,14}. If it’s not found, it would return {NSNotFound, 0}. Let’s use that to check to see if the range was found. We’ll use NSMakeRange to create the NSNotFound range and NSEqualRanges to compare them:

if(NSEqualRanges(NSMakeRange(NSNotFound, 0), [absoluteURL rangeOfString:@"my_substring"])){   NSLog(@"my_substring not found in absoluteURL %@", absoluteURL);}
  • G5nsq5t92cP7hplXVJ7X3zekH66td6Pl

    Also can write this:

    if([absoluteURL rangeOfString:@"my_substring"].location == NSNotFound)){
    NSLog(@”my_substring not found in absoluteURL %@”, absoluteURL);
    }

  • Bergamot

    “It’s times like this, that I miss ruby.”

    Only times like this?

    I love my Mac, I love my iPhone, there are even parts of Cocoa that are nice, but there isn’t a moment I don’t wish that Objective C were just a little bit more like Python or Ruby.

  • Mark Pemburn

    Thanks! I’m an Objective-C and it took a lot of searching around to find out how to do this simple operation! My implementation seems to work the opposite of expectation, however. Strings containing the substring return the integer value of the substring’s location within the string being tested (deviceString) and the non-matches return 0×7fffffff. Given that, you’d thing the following snipped would require != to pass the test. I guess some things are not for man to know . . .

    if([deviceString rangeOfString:@"tester"].location == NSNotFound)
    [deviceStringArray addObject: deviceString];

  • http://murfy.de murphy

    well, I don’t understand your Ruby code…searching for a substring is just:

    absolute_url[substring] #=> nil or the substring

    but you’re absolutely right, the Objective-C APIs are sometimes pathetical…

  • http://shadowfiend.posterous.com/ Antonio

    If we want to be pedantic, perhaps the most idiomatic way to check for substring presence is to use the =~ operator, as in absolute_url =~ /my_regex/ .

  • someone older than u

    Perhaps, instead of complaining about missing a high-level crutch “language” like Ruby, you should just be a real programmer, and write your own regex library in a real low-level language like c… Oh yeah, you can’t.

  • http://brandontreb.com brandontreb

    Dang,

    Someone took the corporate programmer route instead of following his dreams of working for himself. What level are you up to there chief?

    BTW- I can ;)

  • Christine

    Really? That’s how you want to spend your day? Comparing schlongs and rewriting the wheel?

  • wojtek

    Nice ;) I’m with Christine, reinventing the wheel is definitely not the way to go. As for “real programmers” – good for you :D

  • cdann

    if ( [absoluteURL rangeOfString:@"my_substring"].location == NSNotFound )
    {
    NSLog(@”my_substring not found”);
    }

  • Sarokhatch

    can someone recommend a good book on web services.  I am not new to iphones but I am new to web services.
    Thanks

  • Kalyan711987

    how to implement soap web service

  • kam_iOS

    Hi Folks,
    is there any code available  for showing how to upload a file to a Server using Soap?

  • kam_iOS

    Hi Folks,
    is there any code available  for showing how to upload a file to a Server using Soap?

blog comments powered by Disqus