Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: what is the right way to split up a [String] resulting in a [[String]] with a given subarray size?

Starting with a large [String] and a given subarray size, what is the best way I could go about splitting up this array into smaller arrays? (The last array will be smaller than the given subarray size).

Concrete example:

Split up ["1","2","3","4","5","6","7"] with max split size 2

The code would produce [["1","2"],["3","4"],["5","6"],["7"]]

Obviously I could do this a little more manually, but I feel like in swift something like map() or reduce() may do what I want really beautifully.

like image 223
Jordan Smith Avatar asked Oct 16 '14 03:10

Jordan Smith


People also ask

How to split a string to an array in Swift?

To split a string to an array in Swift by a character, use the String.split (separator:) function. However, this requires that the separator is a singular character, not a string. To split a string to an array by a separator string, use String.components (separatedBy:) function.

Is there a limit on the number of splits in Swift?

If not provided, there is no limit on the number of splits. omittingEmptySubsequences (optional) - specifies whether to omit empty string elements or to include them Note: If maxSplits is specified, the array will have a maximum of maxSplits + 1 items. var text = "Swift is awesome. Swift is fun." // split at period "."

How to split a string into an array using a separator string?

To split a string into an array by using a separator string, call the String.components (separatedBy:) function. Here the argument separatedBy can be either one of the following:

How do you split a string in Python?

The split () method breaks up a string at the specified separator and returns an array of strings. // split the text from space print (text. split (separator: " " )) string.split(separator: Character, maxSplits: Int, ommittingEmptySequence: Bool) Here, string is an object of the String class. separator - Delimiter at which splits occur.


1 Answers

In Swift 3/4 this would look like the following:

let numbers = ["1","2","3","4","5","6","7"] let chunkSize = 2 let chunks = stride(from: 0, to: numbers.count, by: chunkSize).map {     Array(numbers[$0..<min($0 + chunkSize, numbers.count)]) } // prints as [["1", "2"], ["3", "4"], ["5", "6"], ["7"]] 

As an extension to Array:

extension Array {     func chunked(by chunkSize: Int) -> [[Element]] {         return stride(from: 0, to: self.count, by: chunkSize).map {             Array(self[$0..<Swift.min($0 + chunkSize, self.count)])         }     } } 

Or the slightly more verbose, yet more general:

let numbers = ["1","2","3","4","5","6","7"] let chunkSize = 2 let chunks: [[String]] = stride(from: 0, to: numbers.count, by: chunkSize).map {     let end = numbers.endIndex     let chunkEnd = numbers.index($0, offsetBy: chunkSize, limitedBy: end) ?? end     return Array(numbers[$0..<chunkEnd]) } 

This is more general because I am making fewer assumptions about the type of the index into the collection. In the previous implementation I assumed that they could be could be compared and added.

Note that in Swift 3 the functionality of advancing indices has been transferred from the indices themselves to the collection.

like image 158
Tyler Cloutier Avatar answered Oct 08 '22 01:10

Tyler Cloutier