C# to Objective-C - Part 1 : Strings

Stephen Zaharuk / Monday, November 19, 2012

My background was originally C# where i worked on a variety of platforms such as ASP.Net, Silverlight, WPF, etc..  So after spending 6+ years working mainly in C#, I was both excited and hesitant to take a look at Objective-C. The first few months i spent a lot of time switching back and forth between Google and XCode(the IDE for iOS), as i familiarized myself with the syntax and API's of this "foreign" language. And although it wasn't nearly as hard as i thought it would be, it would have saved me a lot of time if there was a centralized place where i could get all the information i needed.  Thus i've decided to share what i've learned in this series of posts, where i'll take you step by step through some of the areas that slowed me down, to make your experience a much smoother one.

Today we'll start out simple, strings. Generally the first app that anyone ever writes when they start a new language or platform is "Hello World".  Well this is a tricky task if you don’t know how to create a string in the language.  In this post i’ve tried to compile a list of the most common things people do with strings.  

Declaring a string

string name = “SteveZ”;

NSString* name = @”SteveZ”; // (Note the @ sign, it needs to be included for any string declaration)

Converting an integer to string

string age = i.ToString();

NSString* age = [NSString stringWithFormat:@"%d", i];

Combining a string

string firstName = “Steve”;
string lastName = “Z”;
string fullName = firstName + ” ” + lastName;

NSString* firstName = @”Steve”
NSString* lastName = @”Z”;
NSString* fullName = [NSString stringWithFormat: @"%@ %@", firstName, lastName];

Comparing strings

bool isEqual = firstName == lastName;

BOOL isEqual = [firstName isEqualToString: lastName];

Checking null or empty

bool empty = string.IsNullOrEmpty(firstName);

BOOL empty = (firstName == nil || firstName.length == 0);


string name = firstName.ToLower();
string name = firstName.ToUpper();

NSString* name = [firstName lowercasestring];
NSString* name = [firstName uppercasestring];

Formatting a float

float x = 2.43534f;
string s = x.ToString(“n2″);

float x = 2.34454f;
NSString* s = [NSString stringWithFormat: @"%.2f", x];

I hope this was helpful! In the next post i'll walk you though another common data structure: Arrays.

Also, If you're brand new to the platform or even a seasoned veteran, you should check out our brand new iOS product NucliOS.

You can check out some of the awesome power of the product by checking out our free sample app:

By Stephen Zaharuk (SteveZ)