Objective-C String

April 8, 2009 § Leave a comment

You can add or append another string using

stringByAppendingString: method.
NSString *helloString = @"Hello";
NSString *helloWorldString = [hString stringByAppendingString:@", world!"];

Other methods initWithFormat:, stringWithFormat:, and stringByAppendingFormat: are available.

You can extract substrings from the beginning or end of a string or from a specific range using
  • substringToIndex:
  • substringFromindex:
  • substringWithRnage;

NSString *source = @”0123456789″;
NSString *firstThree = ;
// firstThree is @”012″

NSString *allExceptFirstThree = ;
// allExceptFirstThree is @”3456789″

NSString *twoToFiveRange = NSMakeRange(2,3);
NSString *twoToFive = ;
// twoToFive is @”234″

NSArray *split = ;
// split array contains {@”0123″, @”789″}

You can split a string into substring based on a separator string using componentsSeparatedByString: method.

If you plan to extra strings using pattern-matching approach instead of an index, you should consider using scanner.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Objective-C String at Web Builders.

meta

%d bloggers like this: