3

I have a string array variable.

private string[] _documents;

Values to it are assigned like this.

string[] _documents = { "doc1", "doc2", "doc3" };

And there's a method which adds a string to that array and returns it.

public string GetMail()
{
    return originalMessage + " " + _documents[0];
}

Defining _documents[0] returns only the first element of the array.

How can I return all the elements in the array? (Kinda like what implode function does in PHP)

3 Answers 3

7

I am not familiar with PHP, but you can concatenate all elements of a string array with string.Join:

return string.Join(" ", docs);

The first parameter is the separator; you can pass an empty string if you do not need separators.

Sign up to request clarification or add additional context in comments.

Comments

1
public string GetMail()
{
    return originalMessage + string.Join(" ", _documents);
}

Comments

0

Loop over them and append each to the original string.

 public string GetMail(string[] docs, string originalMessage)
 {
        for (int i = 0; i < docs.Length; i++)
              originalMessage = originalMessage + " " + docs[i];

         return originalMessage;
  }

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.