There’s a convenient method for that by the name of substringFromIndex. This example removes the first character:
NSString *complete = @ & quot ; 12345 & quot ; ; NSString *cutOff = [ complete substringFromIndex : 1 ] ; NSLog ( @ & quot ; % @ & quot ; , cutOff ) ; // cutOff is now 2345
substringFromString takes only one parameter which specifies where to start the new string. The first character is 0 based, like in arrays. The parameter can’t be longer than the original string’s length for obvious reasons.
We can use substringWithRange for this, which requires us to define a range:
NSString *something = @ & quot ; 123456789 & quot ; ; NSRange myRange = NSMakeRange ( 2 , 3 ) ; something = [ something substringWithRange :myRange ] ;
// something is now 345
You create a range by specifying where the substring should start (the first character is 0, like in an array), then specify how many characters from there you want to use.
To grab just the first character you’d specify a range like this:
NSString *something = @ & quot ; 123456789 & quot ; ; NSRange myRange = NSMakeRange ( 0 , 1 ) ; something = [ something substringWithRange :myRange ] ;
// something is now 1
We can use the substringToIndex method for this, using the length of our original string minus 1:
NSString *something = @ & quot ; 12345 & quot ; ; something = [ something substringToIndex : ( something . length - 1 ) ] ; // something is now 1234
Imagine you had an NSString and wanted to save it as an integer value.
You can use the intValue method for that:
NSString *myString = @ & quot ; 47 & quot ; ; int myInt = [ myString intValue ] ;
To do the reverse, you can use the initWithFormat method:
int myInt = 32 ; NSString *myOtherString = [ [ NSString alloc ] initWithFormat : @ & quot ; % i & quot ; , myInt ] ;
Imagine we had a file called myfile.txt which is a standard text file. On each line we have a new item we’d like to read so that our app can do something with it. Here’s how we do that:
// get a reference to our file NSString *myPath = [ [ NSBundle mainBundle ] pathForResource : @ & quot ; myfile & quot ; ofType : @ & quot ; txt & quot ; ] ;
// read the contents into a string NSString *myFile = [ [ NSString alloc ] initWithContentsOfFile :myPath encoding :NSUTF8StringEncoding error :nil ] ;
// display our file NSLog ( & quot ; Our file contains this : % @ & quot ; , myFile ) ;
It is likely that instead of a long list of text we’d rather have each line in a separate string.
Thankfully there’s a method for this:
// split our string into an array NSArray *mySplit = [ myFile componentsSeparatedByString : @ & quot ; \ n & quot ; ] ;
// display any one of them like this NSLog ( @ & quot ; Third Position : % @ & quot ; , [ mySplit objectAtIndex : 2 ] ) ;
Say you had two NSString objects, firstString and secondString and you’d like to combine them into a new string called comboString. You can use the convenience method stringWithFormat for this:
NSString *comboString = [ [ NSString alloc ] initWithFormat : @ & quot ; First % @ and second % @ & quot ; , firstString , secondString ] ;
Any variable is passed at the end of the string, replacing each %@ symbol.
I keep forgetting how to do this so here it is: if you have an integer and you want to print it out, say in a UILabel you can use the initWithFormat method of NSString like so:
NSString *myNumber ; myNumber = [ [ NSString alloc ] initWithFormat : @ & quot ; % d & quot ; , myInteger ] ; self . myLabel . text = myNumber ;
The secret here is the @”%d” which is called a String Format Specifier (search for it in the Developer Docs).
This is really simple in PHP, however it’s not obvious in Objective-C because you’re not printing the strings directly. Rather you print two objects that point to each string. There is a method though which has the same effect: stringByAppendingString. Here’s how you use it:
self . myLabel . text = [ @ & quot ; This is & quot ; stringByAppendingString : @ & quot ; my label & quot ; ] ;
It gets a bit more complex when you want to add three or more together; each stringByAppendingString expression needs to be in [brackets] before you add another one, like so:
self . myLabel . text = [ [ @ & quot ; This is & quot ; stringByAppendingString : @ & quot ; my label & quot ; ] stringByAppendingString @ & quot ; which is rather complex & quot ; ] ;