Split an NSString to access one particular piece
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To split an NSString (Objective-C) or String (Swift) and access a specific piece, use componentsSeparatedByString: in Objective-C or .components(separatedBy:) in Swift. These methods split the string at every occurrence of the delimiter and return an array of substrings. Access the desired piece by index. For more complex splitting (multiple delimiters, regex patterns), use componentsSeparatedByCharactersInSet: or NSRegularExpression. This article covers both Objective-C and Swift approaches with practical examples.
Objective-C: componentsSeparatedByString
Objective-C: componentsSeparatedByCharactersInSet
Split on multiple delimiters at once:
Swift: components(separatedBy:)
Swift: split() Method
Swift's split() is more flexible and returns Substring (avoiding copies):
Practical Examples
Parse a URL
Parse a File Extension
Parse Key-Value Pairs
Common Pitfalls
- Accessing an out-of-bounds index after split: If the delimiter is not found, the array contains the original string as a single element. Accessing
components[1]when the delimiter does not exist causes an index-out-of-range crash. Always checkcomponents.countbefore accessing by index. - Forgetting that
componentsSeparatedByString:creates empty strings for consecutive delimiters: Splitting"a,,b"by","produces["a", "", "b"]. Filter empty strings withNSPredicate(Objective-C) or.filter { !$0.isEmpty }(Swift). - Confusing
split()andcomponents(separatedBy:)in Swift:split()returns[Substring]and omits empty subsequences by default.components(separatedBy:)returns[String]and keeps empty strings. Choose based on whether you want empty strings filtered. - Using string splitting to parse URLs instead of
URLComponents: Splitting URLs by/,?, or&is fragile. UseURLComponentsfor reliable URL parsing that handles encoding, edge cases, and optional components correctly. - Not trimming whitespace after splitting: Splitting
"Alice , Bob , Charlie"by","produces["Alice ", " Bob ", " Charlie"]with leading/trailing spaces. Apply.trimmingCharacters(in: .whitespaces)to each component.
Summary
- Use
componentsSeparatedByString:(Objective-C) or.components(separatedBy:)(Swift) to split on a single delimiter - Use
componentsSeparatedByCharactersInSet:orCharacterSetto split on multiple delimiters - Use Swift's
.split(separator:)for efficient splitting that omits empty subsequences and returnsSubstring - Always check
components.countbefore accessing by index to avoid out-of-bounds crashes - Filter empty strings and trim whitespace after splitting for clean results
Related reading
- Spring Boot with Kotlin - Value annotation not working as expected
- Spring Data JPA How to use Kotlin nulls instead of Optional
- Sqlite File Location Core Data
- Sqlite File Location Core Data
- Standard Android Button with a different color
- Standard Android menu icons, for example refresh
- Start an Activity with a parameter
- START_STICKY and START_NOT_STICKY
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.