Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 20 / 68

Splitting and joining strings

This lesson covers splitting and joining strings in Objective-C, with the aim of handling CSV-style data.

componentsSeparatedByString: splits a string into an NSArray. Conversely, componentsJoinedByString: gathers an NSArray into a single string. Both come up constantly when working with CSV-style data.

The sample code splits a comma-separated string into an array with [csv componentsSeparatedByString:@","] and rebuilds it as a slash-separated string with [fruits componentsJoinedByString:@" / "]. The method names are long but they say what they do.

A common early stumble is exactly that length. Objective-C method names tend to be more descriptive and longer than in other languages, and the typing takes getting used to.

In professional work, this is a standard pairing that shows up whenever CSV files are read or logs are parsed.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)