3

I'm trying to merge array of string with \n, but didn't get any solution. I have following types of array which contains section names.

let arrSectionName = [
    "Section 1",
    "Section 2",
    "Section 3",
    "Section 4",
    "Section 5",
    "Section 6",
]

Now, I want this string from above array:

"Section 1 \nSection 2 \nSection 3 \nSection 4 \nSection 5 \nSection 6"

So Finally message should display as follow:

Alert Message Output:

    Following section which are still pending to complete, please verify and try again:
    Section 1
    Section 2
    Section 3
    Section 4
    Section 5
    Section 6

Section name are dynamic, which will be change runtime and store in arrSectionName.

4

1 Answer 1

7

You should use joined(separator:). It joins the array elements and adds the given separator string between the elements.

let str = arrSectionName.joined(separator: " \n")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.