Sunday, January 18, 2009

Objective-C versus C#, part 1: Searching for a substring within a string

 

I’ve been wanting to create a series of comparisons between the syntax of Objective-C and C#.  I’ve been writing a lot of both these days, so I have a lot of experience with each.  For our first example, let’s say we wanted to check for a substring within a string:

Objective-C

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

I don’t know how you could get any more intuitive than that, but just for kicks let’s look at the C# equivalent:

C#

if (!myString.Contains(“mySubstring”))
{
    Console.WriteLine(“mySubstring not found in myString: “ + myString);
}

No comments: