Swift: How to Split Up a [String] Resulting in a [[String]] With a Given Subarray Size

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

I wouldn't call it beautiful, but here's a method using map:

let numbers = ["1","2","3","4","5","6","7"]
let splitSize = 2
let chunks = numbers.startIndex.stride(to: numbers.count, by: splitSize).map {
numbers[$0 ..< $0.advancedBy(splitSize, limit: numbers.endIndex)]
}

The stride(to:by:) method gives you the indices for the first element of each chunk, so you can map those indices to a slice of the source array using advancedBy(distance:limit:).

A more "functional" approach would simply be to recurse over the array, like so:

func chunkArray<T>(s: [T], splitSize: Int) -> [[T]] {
if countElements(s) <= splitSize {
return [s]
} else {
return [Array<T>(s[0..<splitSize])] + chunkArray(Array<T>(s[splitSize..<s.count]), splitSize)
}
}

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

I wouldn't call it beautiful, but here's a method using map:

let numbers = ["1","2","3","4","5","6","7"]
let splitSize = 2
let chunks = numbers.startIndex.stride(to: numbers.count, by: splitSize).map {
numbers[$0 ..< $0.advancedBy(splitSize, limit: numbers.endIndex)]
}

The stride(to:by:) method gives you the indices for the first element of each chunk, so you can map those indices to a slice of the source array using advancedBy(distance:limit:).

A more "functional" approach would simply be to recurse over the array, like so:

func chunkArray<T>(s: [T], splitSize: Int) -> [[T]] {
if countElements(s) <= splitSize {
return [s]
} else {
return [Array<T>(s[0..<splitSize])] + chunkArray(Array<T>(s[splitSize..<s.count]), splitSize)
}
}

Split string to arrays with maximum variables in each array

Step 1 - get fully separated array:

let numbers = "12,3,5".components(separatedBy: ",")

Step 2 - chunk your result to parts with ext:

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)])
}
}
}

let chunkedNumbers = numbers.chunked(by: 10)

Step 3:

let stringsArray = chunkedNumbers.map { $0.joined(separator: ",") }

Result: ["12,3,5,75,584,364,57,88,94,4", "79,333,7465,867,56,6,748,546,573,466"]

Link to gist playground.

Split string by two symbols in Swift

A simple while loop:

let str = "df57g5df7g"

var startIndex = str.startIndex
var result = [String]()

repeat {
let endIndex = startIndex.advancedBy(2, limit: str.endIndex)
result.append(str[startIndex..<endIndex])

startIndex = endIndex
} while startIndex < str.endIndex

print(result)

Or something more Swifty:

let result = 0.stride(to: str.characters.count, by: 2).map { i -> String in
let startIndex = str.startIndex.advancedBy(i)
let endIndex = startIndex.advancedBy(2, limit: str.endIndex)
return str[startIndex..<endIndex]
}

Split a large Vector Equally in Swift

Yes, You can do this with an extension which you can also use it as a general solution:

extension Array {
func splited(into size: Int) -> [[Element]] {
return stride(from: 0, to: count, by: size).map {
Array(self[$0 ..< Swift.min($0 + size, count)])
}
}
}
let yourArray = [14.78125,-0.6308594, ...] // 15360 elements of your array
let result = yourArray.splited(into: 128)

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

I wouldn't call it beautiful, but here's a method using map:

let numbers = ["1","2","3","4","5","6","7"]
let splitSize = 2
let chunks = numbers.startIndex.stride(to: numbers.count, by: splitSize).map {
numbers[$0 ..< $0.advancedBy(splitSize, limit: numbers.endIndex)]
}

The stride(to:by:) method gives you the indices for the first element of each chunk, so you can map those indices to a slice of the source array using advancedBy(distance:limit:).

A more "functional" approach would simply be to recurse over the array, like so:

func chunkArray<T>(s: [T], splitSize: Int) -> [[T]] {
if countElements(s) <= splitSize {
return [s]
} else {
return [Array<T>(s[0..<splitSize])] + chunkArray(Array<T>(s[splitSize..<s.count]), splitSize)
}
}

How to pair strings together from NSArray?

Here is something that tries to be fast, by using the array's iterator and by making heavy use of arrayWithCapacity.

        NSArray *array = @[ @"1.100.2", @"23465343", @"1.100.1", @"46535334", @"1.0.03", @"24353454" ];
NSUInteger countPerPair = 2;
NSMutableArray * pair = [NSMutableArray arrayWithCapacity:countPerPair];
NSMutableArray * pairedArray = [NSMutableArray arrayWithCapacity:( array.count + countPerPair / 2 ) / countPerPair];

for ( NSObject * i in array )
{
if ( pair.count == countPerPair )
{
[pairedArray addObject:pair];
pair = [NSMutableArray arrayWithCapacity:countPerPair];
}

[pair addObject:i];
}

if ( pair.count )
{
[pairedArray addObject:pair];
}


Related Topics



Leave a reply



Submit