Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append an array of strings to a string in Swift

I have an array of strings for one variable, and a string as another variable. I'd like to append all of the strings in the collection to the single string.

So for example I have:

 var s = String()

   //have the CSV writer create all the columns needed as an array of strings
   let arrayOfStrings: [String] = csvReport.map{GenerateRow($0)}

// now that we have all the strings, append each one 
        arrayOfStrings.map(s.stringByAppendingString({$0}))

the line above fails. I've tried every combination I can think of, but at the end of the day, I can't get it unless I just create a for loop to iterate through the entire collection, arrayOfStrings, and add it one by one. I feel like I can achieve this the same way using map or some other function.

Any help?

Thanks!

like image 832
NullHypothesis Avatar asked Nov 28 '15 18:11

NullHypothesis


People also ask

How do I convert an array to a string in Swift?

In Swift, we are allowed to join the elements of an array in a string using joined() function. This function creates a new string by concatenating all the elements of an array using a specified separator. We can also join all the elements of an array without any separator.

How do I merge two arrays in Swift?

var Array1 = ["Item 1", "Item 2"] var Array2 = ["Thing 1", "Thing 2"] var Array3 = Array1 + Array2 // Array 3 will just be them combined :) Save this answer.


1 Answers

You can use joined(separator:):

let stringArray = ["Hello", "World"]
let sentence = stringArray.joined(separator: " ")  // "Hello World"
like image 138
Leo Dabus Avatar answered Oct 05 '22 11:10

Leo Dabus