How to properly messageBox contents of String.Format({0}, Array)?

I’m trying to display the contents of an array as part of the String.Format function:

Message Box Code:
message box code

Places Code:

places type

However, the contents in the string are not being displayed individuals, but as a group:

Please feel free to download the project from my github:

Could you please tell me where I’m off?

Thank you.



String format you need to pass the strings, not the string array

Check below to get your result

You need to pass the index of the string if it is in Array

Hope this helps you



Hi Ksrinu,

Sorry I wasn’t more specific before. I’d like to display all the contents of the array without declaring a specific index.

If this isn’t possible with an array, could it be done with a different type of object?

What I’d like to accomplish is a clean, simplistic method for display all the contents without having to use a ForEach loop. I saw something like this in the microsoft documentation on string.format(): Microsoft Documentation

The function you refer to in the documentation won’t solve your issue since your have to specify all the indexes in the composite format string either way. E.g. if your array has three items.

String.Format("Places: {0}, {1}, {2}", places)

If you want to display the whole array, use String.Join() instead:

String.Format("{0} from: {1}", study, String.Join(", ", places))

Yes! An simple and elegant solution that was overlooked by many that I’ve interacted with. Thank you ptrobot.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.